How do I compare two arrays of DataRow objects in PowerShell? How do I compare two arrays of DataRow objects in PowerShell? powershell powershell

How do I compare two arrays of DataRow objects in PowerShell?


I wrote a script to do this a little while back. The script (Compare-QueryResults.ps1) is available here and you will also need my Run-SQLQuery script (available here) or you can replace that with a script or function of your own.

Basically, what the script does is take the results of each of your queries and break the datarows apart so that each field is its own object. It then uses Compare-Object to check for any differences between the data in those rows. It returns a comparison object that shows you all the differences between the data returned.

The results are an object, so you can save them to a variable and use Sort-Object or the Format-* cmdlets with them.

Good luck. If you have any problems with the scripts, let me know, I'd be happy to walk you through them. I've been using them for application testing, seeing what rows are being modified by different actions in a program.


To simply compare two System.Data.DataRow, you can do something like this:

foreach ($property in ($row1 | Get-Member -MemberType Property)) {    $pName = $property.Name    if ($row1.$pName -ne $row2.$pName) {        Write-Host "== $pName =="        $row1.$pName        $row2.$pName    }}


Do you need two arrays of DataRows? the DataRow object has a RowState property which will give you what you require. See the MSDN Docs: http://msdn.microsoft.com/