Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Compare CSV files and export to a new CSV file.

$
0
0

I am trying to compare two CSV files with Powershell, and export the differences that appear in the second CSV to a new CSV file.

My attempt at a script is as follows:

$today = [DateTime]::Today.ToString("MM-dd-yyyy")
$yesterday = [DateTime]::Today.AddDays(-1).ToString("MM-dd-yyyy")
$yesterdays = Import-Csv -Path ($yesterday +".csv")
$todays = Import-Csv -Path ($today +".csv")
compare-object $yesterdays $todays -Property EMPLID -PassThru| Where-Object {$_.SideIndicator -eq '=>'} | Export-Csv .\NewUsers.csv -NoTypeInformation
compare-object $yesterdays $todays -Property GROUP_NAME -PassThru| Where-Object {($_.GROUP_Name -eq 'Students14Spring' -or 'FacStaffStudents14Spring') -and $_.SideIndicator -eq '=>'} | Export-Csv .\UpdatedUsers.csv -NoTypeInformation

The first compare seems to work as expected, but the second one delivers incorrect results.  I would also like to change the second compare to check if a GROUP_NAME from the first file was changed to a specific group name in the second file.  I am not sure how to do this.  Get-content seems to work better with compare-object than import-csv but I am not sure how to correctly export the contents into a CSV.  The compare object seems to compare by column I would like the comparison to be line by line.  Basically I want it to read each line on the $today csv and parse through every line on $yesterday csv. If a line is new, add it to the new CSV. If the GROUP_NAME has been changed from one of two specific names to another one of two specific other group names,  add to the new CSV as well.  Any help would be appreciated.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>