Archive

Posts Tagged ‘powershell csv diff’

Powershell Script to Diff CSV Data

November 4, 2014 Leave a comment

This script can be used to search for differences in CSV files based on a cell value and export the rows that are different to a new file. The steps are as follows:

  1. Import file1 – the file that has data to diff against.
  2. Convert the variable to an array based on the cell value you want to diff against.
  3. Import file2 – the file you want to get the diff data from.
  4. Set the export file name.
  5. For each line in file2, if the cell value is not contained in the file1 array variable, export the line to the export file.

You can copy and paste the code below or you can download the script here.


$file1 = ipcsv “.\1.csv”
$file1 = [array]$file1.No
$file2 = ipcsv “.\2.csv”
$file3 = “.\3.csv”


$file2 | %{if ($file1 -notcontains $_.No){$_ | epcsv $file3 -append -notype} }

 

Notes:

This script is about as simple as I could make it. There are obviously a ton of things you can do with this functionality. The concept is provided to manipulate according to your needs.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.

Advertisements
Categories: Scripts Tags: