r/PowerShell • u/greenstarthree • Oct 07 '26
Question Compare-Object - return only difference object entries of CHANGED rows in files
Hi all,
I'm using Compare-Object to basically compare two CSV files, and return additions, changes, and removals.
Essentially I'm runnning:
(Note that "@files" is only in quotes to stop Reddit converting it to a user link!)
$files = @{ReferenceObject = (Get-Content -Path C:\file1.csv)DifferenceObject = (Get-Content -Path C:\file2.csv)}Compare-Object "@files" | export-csv C:\differences -NoTypeInformation
Lets say the files look like this:
File 1Cat,Brown,4Dog,Black,4Horse,White,4
File 2Cat,GINGER,4Dog,Black,4Duck,Grey,2
Duck has been added in file2 (not present in file1)
Horse has been removed in file2 (was present in file1)
Cat is changed from file1 to file2.
The compare-object gives me a list of changes in a new file.
- Entries found in file2 but not in file1 (Duck) are marked with side indicator =>
- Entries found in file1 but not in file2 (Horse) are marked with side indicator <=
- However, entries that are still present in both files but have had one or more of their columns changed (Cat) are present in the results file with both side indicators.
I understand this correct given how Compare-Object works, but it leads to a problem when I try to create separate "Additions/Changes" and "Removals" files based on the results.
For example, to see only the items Added or Changed in file2 (Duck and Cat), I can use:
?{$_.SideIndicator -eq '=>'}
To show only the difference object results.
The problem comes when I use the opposite side indicator:
?{$_.SideIndicator -eq '<='}
As this gives me the removed item I need, (Horse), but also gives me the changed item (Cat) from File1. This gives me no way of telling which of the "<=" entries are removals and which are changes. I'm looking for a way to differentiate between the two.
I hope this makes sense...!
3
u/jimb2 Oct 07 '26 edited Oct 07 '26
Compare-Object just does a flat compare without any data diferentiation. In your case, the animal type is actually a unique key value (if I understand correctly.) There is only one animal of each type.
You can use a group-object to link the compare result by animal type
If count is 1 use sideindicator for added/removed/retained
If count is 2 use sideindicator for old and new versions of the animal
5
u/jstar77 Oct 07 '26
Compare-object is one of the most frustrating functions. The info below may not be 100% correct.
You have to explicitly list properties of the object if you leave out -property I think the behavior you are getting is typical.
If you want to grab all of the properties you can pull them from the first object referenceobject array.
1
u/420GB Oct 08 '26
If you need a proper "smart" diff of two text files, have you considered just using git (git-diff)?
0
u/ankokudaishogun Oct 07 '26
If the animal type is an unique value, you can:
(i'll comment later, not optimized but should work, check the file names)