Need Help with Automating CSV Comparison and Highlighting Differences

Hello there,

I am working on a PowerShell script to automate comparison between two CSV files one representing old data, and the other containing updated records. Both files have identical column headers and a unique identifier column. What I would like to achieve is a comparison that highlights rows where any field value has changed.

Ideally, the output would be a third CSV that includes only the changed rows from the updated file, along with a new column that indicates which fields were modified. I have been able to do a basic comparison using Compare-Object, but it is not quite granular enough for my use case since it flags entire rows rather than showing field-level changes.

Has anyone tackled something similar or could suggest a PowerShell-native way to accomplish this? I am trying to avoid bringing in external modules if possible.

Thanks in advance for your help and assistance.

How about providing some sample data from before and after and the code you’ve already tried for us to play around with and maybe to come up with something fitting?

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

2 Likes

Definitely with @Olaf that some example code would be helpful. I have the memory of a goldfish so I can’t tell you if I’ve done this before, but here’s my quick thoughts.
Example Code:

$Thing1 = [PSCustomObject]@{
    UniqueID = '223b15e9-15ad-405e-a783-bb1f29e9aad8'
    Prop1 = 1
    Prop2 = 2
    Description = "This is a thing"
    Color = "Red"
}

$Thing2 = [PSCustomObject]@{
    UniqueID = '223b15e9-15ad-405e-a783-bb1f29e9aad8'
    Prop1 = 1
    Prop2 = 2
    Description = "This was a thing"
    Color = "Green"
}

If Thing1 and Thing2 represent an individual “row” from an old CSV and a new CSV I can experiment. Using Compare-Object, according to the documentation, will try to convert the objects to something comparable, or it will run ToString() on them and compare. This doesn’t work for us in this example. You can use the -Property parameter to list properties that you want to compare, but if you do so, the output doesn’t really specific which property was different:

PS> compare-object -ReferenceObject $Thing1 -DifferenceObject $Thing2 -Property prop1,prop2,description,Color



prop1         : 1
prop2         : 2
description   : This was a thing
Color         : Green
SideIndicator : =>

prop1         : 1
prop2         : 2
description   : This is a thing
Color         : Red
SideIndicator : <=

Now in my head I start thinking about looping through the properties and using compare-object but that sounds like ‘match’ with extra steps.

I think what you should think about is writing your own function that compares two provided PSCustomObjects based on a known set of properties. You can loop through the properties and compare between the two objects using something like -eq or -match and if they aren’t the same then you can take action.

1 Like