Powershell Compare two csv

Hello All,

I have two csv file bck and rpt.csv

bck.csv

Type Specification Status
Backup Test Completed
Backup Test1 Completed
Backup Test2 Completed
Backup Test3 Completed

rpt.csv

Type Specification Status
Backup Test Completed
Backup Test1 Failed
Backup Test2 Completed
Backup Test3 Aborted

I looking for compare both file and update the status in bck.csv file.

Anyone help on this. Thanks!

This will update lines in bck.csv as well as add new lines if rpt.csv has new lines for additional tests that aren’t present in bck.csv.

$bck = Import-Csv .\bck.csv
$rpt = Import-Csv .\rpt.csv

$diffs = Compare-Object -ReferenceObject $bck -DifferenceObject $rpt -Property Specification,Status -PassThru

foreach ($diff in $diffs) {
    if ($bck.Specification -contains $diff.Specification) {
        ($bck | Where-Object { $_.Specification -eq $diff.Specification }).Status = $diff.Status
    }
    else {
        $bck += New-Object PSObject -Property @{Type = $diff.Type; Specification = $diff.Specification; Status = $diff.Status }
    }
}

$bck | Export-Csv .\bck.csv -Force