Monday, April 14, 2014

Powershell - Compare 2 csv files and list changes

I had huge list of data and require to find out changes. (Sample) As you can see I have 2 csv file with New.csv and old.csv

Below is the inner data of both files. There are some changes between both files.

After running below script on the both file

 $OldCSV = Import-Csv -Path C:\temp\CSV-test\Old.csv   
 $NewCSV = Import-Csv -Path C:\temp\CSV-test\New.csv  
 $output = @()  
   forEach ($Column in $OldCsv) {      
     $result = $NewCSV | Where-Object {$Column.Name -eq $_.Name}  
     $CPU = if ($Column.CPU -ne $result.CPU) {"Found Change"}  
     $Powerstate = if ($Column.Powerstate -ne $result.PowerState) {"Previous: " + $Column.powerstate + " | Now:" + $result.powerstate}  
     $output += New-object PSObject -property @{  
       VMname = $Column.Name  
       Powerstate = $powerstate  
       CPU = $cpu  
 $output | select-object vmname, CPU, Powerstate | Export-Csv -Path C:\temp\CSV-test\Changes.csv -NoTypeInformation  

Below is the result.

Warning: All the testings are performed in lab environment.

No comments: