Compare Two CSV and report on different value

Hi All, Need some help comparing two CSVs and report if the value in one column has a different value to the other CSV. Column1 is the full path of a file and Column2 will be an MD5 hash of that file. I need to compare both and if the filename exists on both but they have different MD5 Hash values I want to report on those filenames fullpath. see screenshot attached … C:\File2.txt has a different MD5 value, so the report should have 3 columns FileNamePath, OldMd5, NewMd5

Actually I managed to write something that does the job, happy to get criticism or feedback on it.

–Script Start–

$TrackList = Import-Csv -Path “C:\Users\Administrator\Documents\TrackList.csv”
$FileList = Import-Csv -Path “C:\Users\Administrator\Documents\FileList.csv”
$FileCheck = $filelist | Where-Object { $_.FullFilePath -in $TrackList.FullFilePath }

$FileReport = @()
Foreach ($File in $FileCheck) {

$FilePath = $File.FullFilePath
$Hash = $File.HashMD5
$OldHash = ($TrackList | Where-Object { $_.FullFilePath -eq $FilePath }).HashMD5

If ($Hash -ne $OldHash) {
    $FileReport += New-Object psobject -Property ([ordered]@{

            FilePath = $($FilePath)
            OldHash  = $($OldHash)
            NewHash  = $($Hash) 
        })
}

}

$FileReport

–Script End–

nic82m
Welcome to thte forums.

If you want to compare two objects (or the content of two CSV files) in PowerShell you can use Compare-Object.

Please read the help completely including the examples to lern how to use it. :wink:

Hi Olaf,

I had a crack using compare-object but then didn’t find a nice way to output the report the way I wanted cause I converted the 2 columns to strings. this what I wrote using compare-object.

$d = Import-Csv -Path “C:\Users\Administrator\Documents\TrackList.csv”
$c = Import-Csv -Path “C:\Users\Administrator\Documents\FileList.csv”

$a = $d | Where-Object {$.FullFilePath -in $c.FullFilePath}
$b = $c | Where-Object {$
.FullFilePath -in $a.FullFilePath}

$a1 = ($a | % { “$($.fullfilepath)>$($.HashMD5)” } ) | Sort-Object
$b1 = ($b | % { “$($.fullfilepath)>$($.HashMD5)” } ) | Sort-Object

Compare-Object $a1 $b1

I’m not completely sure if I got what you actually want to compare but I think something like this should get you started:

$TrackListCSV = Import-Csv -Path 'C:\Users\Administrator\Documents\TrackList.csv'
$FileListCSV = Import-Csv -Path 'C:\Users\Administrator\Documents\FileList.csv'

Compare-Object -ReferenceObject $TrackListCSV -DifferenceObject $FileListCSV -Property 'HashMD5'  -PassThru

BTW: Instead of pictures of your input data you should have posted the raw CSV text formatted as code. So that people willing to help you would be able to copy and use the sample data. :wink:

1 Like