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

gugoeb,
Welcome to the forum. :wave:t4:

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

For your requirement it would be something like this:

$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

1 Like