Compare 2 CSV files and write all differences


I have 3 CSV files that contain user information. CSV1 is a “master” list of all inactive users. CSV2 is a current list of users that need to be deactivated and CSV3 is a list of users that need to be activated.

What I want is to have a PowerShell script that can be called from another script (the one that creates CSV2/3) to have it compare CSV1/2 and write all unique records back to CSV1. Then I want it to compare CSV1/3 and remove all records in CSV1 that exist in CSV3. CSV2/3 can change daily and it is possible to have no data in them, other than the header.

There are several unique fields, but I would want to compare on ‘EmployeeID’.
All 3 CSV files have headers (same headers in all of them, so the data is consistent).

What I have ended up with so far will add the records from CSV2 to CSV1, but it adds both headers.


I don’t know the exact answer but can’t you do something like this?

look at this link for complete example and result : Compare csv with same headers

If you know the differences it is easy enough to write them in the other csv.

I don’t have much experience with compare-objects but since it is a csv you can just delete the column with this.

This command will read your last csv and select the columns you want to keep and export it to a new csv.

Add a remote-item command to remove any csv’s you don’t need anymore and your done.


Compare 2 CSV files and write all differences by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply