Home » How to Compare Two Hashtables in PowerShell

How to Compare Two Hashtables in PowerShell

PowerShell offers a few different ways to compare two hashtables. You can use the Compare-Object cmdlet in PowerShell or converting hash tables to JSON and compare them.

In this article, we will discuss how to compare two hashtables in PowerShell.

Compare Two Hashtables using Compare-Object Cmdlet

Using the Compare-Object cmdlet in PowerShell, you can compare two hashtables. This command compares two hashtables and find out any differences in them.

The following example demonstrates using the Compare-Object cmdlet to compare hashtables.

$hashtable1 and $hashtable2 stores the key and value pairs for hashtables.

The Compare-Object cmdlet in PowerShell used to compare the keys of two hashtables and values of two hashtables and stores the differences in $keyDifference and $valuesDifference variables.

The if-else statement validates the criteria to check if two hashtables are equal or not.

$hashtable1 = @{
    "Key1" = "Value1";
    "Key2" = "Value2";
    "Key3" = "Value3"
}

$hashtable2 = @{
    "Key1" = "Value1";
    "Key2" = "Value2";
    "Key3" = "Value3";
    
}

$keysDifference = Compare-Object -ReferenceObject $hashtable1.Keys -DifferenceObject $hashtable2.Keys
$valuesDifference = Compare-Object -ReferenceObject $hashtable1.Values -DifferenceObject $hashtable2.Values

if ($keysDifference -or $valuesDifference) {
    Write-Host "Hashtables are not equal"
if ($keysDifference) {
        Write-Host "Keys:"
            $keysDifference | ForEach-Object {

            if ($_.SideIndicator -eq "<=") {
                    Write-Host "    Missing in the second hashtable: $($_.InputObject)"
            }          
            else {
                    Write-Host "    Missing in the first hashtable: $($_.InputObject)"
            }
        }
    }
 
if ($valuesDifference) {
        Write-Host "Values:"
        $valuesDifference | ForEach-Object {
        
        if ($_.SideIndicator -eq "<=") {
            Write-Host "    Missing in the second hashtable: $($_.InputObject)"
        } 
        else {
            Write-Host "    Missing in the first hashtable: $($_.InputObject)"
         }
       }
    }
} 
else {
    Write-Host "Hashtables are equal"
}

Result:

Hashtables are equal

Compare Two Hashtables Using the Converting them to JSON and Compare

You can compare two hash tables by converting them to JSON strings using ConvertTo-Json and then compares the JSON strings. If the JSON strings are equal, the hash tables are considered identical.

$hashTable1 = @{
    Key1 = "Value1"
    Key2 = "Value2"
}

$hashTable2 = @{
    Key1 = "Value1"
    Key2 = "Value3"
}

$hashTable1Json = $hashTable1 | ConvertTo-Json
$hashTable2Json = $hashTable2 | ConvertTo-Json

if ($hashTable1Json -eq $hashTable2Json) {
    Write-Host "Hash tables are identical"
} else {
    Write-Host "Hash tables are different"
}

Result:

Hash tables are different.

Conclusion

I hope the above article on how to compare two hashtable in PowerShell using the Compare-Object cmdlet is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.