# Compare two arrays and only get objects that are not part of the other one

**URL:** <https://forums.powershell.org/t/compare-two-arrays-and-only-get-objects-that-are-not-part-of-the-other-one/17919>\
**Category:** PowerShell Help\
**Created:** [November 10, 2021, 3:54pm UTC](https://forums.powershell.org/t/compare-two-arrays-and-only-get-objects-that-are-not-part-of-the-other-one/17919 "2021-11-10T15:54:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gugoeb](https://avatars.discourse-cdn.com/v4/letter/g/e19adc/32.png) [@gugoeb](https://forums.powershell.org/u/gugoeb)\
**Post date:** [November 10, 2021, 3:54pm UTC](https://forums.powershell.org/t/compare-two-arrays-and-only-get-objects-that-are-not-part-of-the-other-one/17919/1 "2021-11-10T15:54:21Z")

</div>

Hi,

I currently have two arrays

array1  
array2

I want to have all objects of array1 that are not part of array2

Im using this::  
$array3 = $array1 | Where {$array2.ObjectID -NotContains $\_.ObjectID}

This is almost working except for one issue:  
This also adds objects of array2 that are not an object of array1 to array3, but this is NOT what i want. I want only to add objects of array 1 if they are not an object within array2

What am I doing wrong here?

BG  
Gunnar

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 10, 2021, 4:11pm UTC](https://forums.powershell.org/t/compare-two-arrays-and-only-get-objects-that-are-not-part-of-the-other-one/17919/2 "2021-11-10T16:11:59Z")

</div>

gugoeb,  
Welcome to the forum. 👋🏽

In PowerShell we work with objects and properties. To compare objects we use

> **[Compare-Object (Microsoft.PowerShell.Utility) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.2)**
>
> The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method,...

For your requirement it would be something like this:

```auto
$Array1 = @'
ObjectID
1
2
3
4
5
'@ | 
ConvertFrom-Csv

$Array2 = @'
ObjectID
1
3
5
6
7
'@ | 
ConvertFrom-Csv

Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2 -Property ObjectID -PassThru | 
    Where-Object -Property SideIndicator -EQ -Value '<='

```

And BTW: When you post code please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your curseor on an empty line, click the button and paste your code.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 10, 2021, 8:21pm UTC](https://forums.powershell.org/t/compare-two-arrays-and-only-get-objects-that-are-not-part-of-the-other-one/17919/3 "2021-11-10T20:21:02Z")

</div>

The example you show would not do what you state. No members of $array2 will end up in $array3. Assuming the object has property ObjectID, it should do exactly what you desire.

```auto
$array1 = 1..4 | ForEach-Object {
    [PSCustomObject]@{
        ObjectID = $_
    }
}

$array2 = 3..6 | ForEach-Object {
    [PSCustomObject]@{
        ObjectID = $_
    }
}

$array3 = $array1 | Where {$array2.ObjectID -NotContains $_.ObjectID}

$array3

ObjectID
--------
       1
       2

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:20pm UTC](https://forums.powershell.org/t/compare-two-arrays-and-only-get-objects-that-are-not-part-of-the-other-one/17919/4 "2024-05-16T20:20:25Z")

</div>


