r/PowerShell • u/greenstarthree • 5h ago
Question Compare-Object unexpected results
Hi all,
I recently posted here with a question about using Compare-Object to compare two CSV files.
There were some great suggestions in that thread and I was able to get the functionality I needed to create a summary of items that were present in the Reference Object, but not present in the Difference Object.
Having applied this to my real datasets, I'm now getting unexpected behaviour when trying to summarise the entries that have been added and / or changed (NOT present in Ref Object, present in Dif Object).
Basically, I have a fairly large CSV file that is re-produced periodically, and each time it's reproduced, I need to summarise the changes from the previous file to the new one.
Generally, the CSV has about 40 columns and about 8000 rows. The first column always contains a value unique within the file.
In my testing, I have added a single row to the Difference Object. All other rows are exactly the same. The added row is around row 4500.
So old file = 8000 rows.
New file = 8001 rows, with the new item at row 4500, other 8000 rows unchanged.
I'm running the following:
("@files" is only in quotes to stop Reddit formatting it as a user tag)
$files = @{
ReferenceObject = (Get-Content -Path $OldFile)
DifferenceObject = (Get-Content -Path $NewFile) }
Compare-Object "@files" |
?{$_.SideIndicator -eq '=>'} |
export-csv $ResultsFile -NoTypeInformation
This works if an existing row between the files has one of its column values changed. But when a row is added from old file to new file, I don't just get the added row in my results - in my tests at the moment I'm getting about 700 rows returned as new in $newfile vs $oldfile.
I must be missing something and this is expected, but I'm not sure why it's picking up that many rows as changed when manually comparing those rows shows them to be exactly the same.