I think you are not completely aware how Compare-Object works. Let me tyr to explain.
The Cmdlet compares two objects, side to side, and it will show you the difference with a side indicator.
For example, I import the contents of 2 csv files in a variable each. But I expand the property called ‘Name’. Why? Because the ‘Name’ property is actually an array.
Here I’m importing the csv file in a variable, without expanding the property:
$1 = Import-Csv C:\temp\1.csv
Then I look up the property type:
$1.GetType()
This will give me this result:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
And when you then compare two objects of the same kind with Compare-Object, this result is being shown:
PS C:\> $1 = Import-Csv C:\temp\1.csv
PS C:\> $2 = Import-Csv C:\temp\2.csv
PS C:\> Compare-Object -ReferenceObject $1 -DifferenceObject $2
InputObject SideIndicator
----------- -------------
@{Name=6} =>
You can see the difference; the object is present in variable $1, but not variable $2. But it’s not very useful, and you can’t use this to export the differences to a separate csv file. Look at this:
PS C:\> Compare-Object -ReferenceObject $1 -DifferenceObject $2 | Select-Object InputObject
InputObject
-----------
@{Name=6}
And this is what you will see when you try to export to a difference file:
PS C:\> Compare-Object -ReferenceObject $1 -DifferenceObject $2 | Select-Object InputObject | Export-Csv -Path C:\Temp\difference.csv -NoTypeInformation
PS C:\> Import-Csv -Path C:\Temp\difference.csv
InputObject
-----------
@{Name=6}
So how do you handle this? With expanding the object property ‘Name’. You do this with Select-Object -ExpandProperty:
$1 = Import-Csv C:\temp\1.csv | Select-Object -ExpandProperty Name
$2 = Import-Csv C:\temp\2.csv | Select-Object -ExpandProperty Name
Next we can check the differences, and the output is much more usable:
PS C:\> Compare-Object -ReferenceObject $1 -DifferenceObject $2
InputObject SideIndicator
----------- -------------
6 =>
Now we export it again to a separate csv file:
Compare-Object -ReferenceObject $1 -DifferenceObject $2 | Select-Object InputObject | Export-Csv -Path C:\Temp\difference.csv -NoTypeInformation
And when we check the file, you have the difference in a readable format:
PS C:\> Import-Csv -Path C:\Temp\difference.csv
InputObject
-----------
6
So the complete solution would be:
$1 = Import-Csv C:\temp\1.csv | Select-Object -ExpandProperty Name
$2 = Import-Csv C:\temp\2.csv | Select-Object -ExpandProperty Name
Compare-Object -ReferenceObject $1 -DifferenceObject $2 | Select-Object InputObject | Export-Csv -Path C:\Temp\difference.csv -NoTypeInformation
Or you can also do it in a oneliner:
Compare-Object -ReferenceObject (Import-Csv C:\temp\1.csv | Select-Object -ExpandProperty Name) -DifferenceObject (Import-Csv C:\temp\2.csv | Select-Object -ExpandProperty Name) | Select-Object InputObject | Export-Csv -Path C:\Temp\difference.csv -NoTypeInformation
Just try to remember, Compare-Object compares every property in the reference object with the difference object. If anything is different, it will show you the side indicator:
=> means property is present in reference object, but not in difference object
<= means property is present in difference object, but not in reference object
== means property is present in both objects (this you would only see when using the parameter -IncludeEqual.
Hope this helps!