Compare two CSVs with PowerShell
Todays topic is simple: compare two multi-column CSVs. First of, we need the CSVs:
(You can click on “raw” in the Gists to see how I structured them.
Now that we got that out of the way, we can start with something fun: PowerShell!
We will have to import both CSVs.
Next up we do something that might sound stupid, but bare with me. Instead of comparing every filed in both CSVs, we simply create a variable per line in those CSVs. So we will create an array list, because we don’t know how long those CSVs might be.
Those array lists want to be filled with content. In the next step, we will move through the CSVs line by line and create a variable out every line. It is important that you provide the header names of each column you want to compare. In my case, as you can see in the CSVs above, I choose the names “header1,header2,header3”. You will have to replace $aItem.header1 with $aItem.YOURHEADER.
Now we have two array lists that we can compare with those little lines of codes.
And we are done. The script will now compare the two CSVs and provide the results in these four variables. You can use them how ever you can see fit. If you want to turn them back to a CSV, simply replace the “_” with a “,” again.
Here’s a link to the repo on Github and the code is below.