r/PowerShell • u/greenstarthree • Oct 15 '26
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 -NoTypeInformationThis 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.
2
u/Ghomari_ahuva Oct 15 '26
this is because Get-Content compares line by line in order. inserting a row at 4500 means every subsequent line is offset by one, so they all mismatch. Try importing as actual CSV objects and comparing on the unique key column instead
6
u/y_Sensei Oct 15 '26
You probably want something similar to this:
1
u/imahe Oct 15 '26
Is there a reason you are using Get-Content and not Import-Csv?
2
u/charleswj Oct 15 '26
Assuming no change in order, probably simpler (and faster) to treat the files as plain text and compare entire lines. Then ConvertFrom-Csv the difference lines.
1
u/lan-shark Oct 15 '26
Compare-Objectand unexpected results. Name a more iconic duo. I'll wait.Honestly I have nothing helpful to add and I'm sure it's a skill issue, but I swear whenever I use
Compare-Objectit's always a pain and a half to get working how I want1
u/purplemonkeymad Oct 15 '26
Compare-Object is order independent so a change in the middle won't cascade down. I can't recreate this myself. Are you able to cut out a small section to see if you can recreate it with a small number of lines?
How are the files created? Is it an automated system? If so is there a possibility of changes to the output without changes to the data?
Can you pick out an "equal" line from the compare and compare the values with Format-Hex to check for hidden characters?
Eg if it's a csv and you edit one in excel, it might save with different quoting rules than the initial generation.
1
u/ankokudaishogun Oct 16 '26 edited Oct 16 '26
Convert the file to CSV instead of using them raw.
It makes comparisons much easier.
Example:
content of $OldFile:
content of $NewFile:
Dog is unchanged
Cat and Horse are changed
Mouse and Duck are new
Hyppo is removed
Script:
Content of the $ResultsFile: