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...!