PowerShell: How to Combine Multiple Hash Tables


Often you may want to combine multiple hash tables into a single hash table in PowerShell.

You can use the following methods to do so:

Method 1: Combine Hash Tables (With No Duplicate Keys)

$new_hash = $hash1 + $hash2

This particular example combines the hash tables named $hash1 and $hash2 into a new hash table named $new_hash.

This method works only if there are no duplicate keys between the two hash tables.

Method 2: Combine Hash Tables (With Duplicate Keys)

Function CombineHash([ScriptBlock]$Operator) {
    $Out = @{}
    ForEach ($h in $Input) {
        If ($h -is [Hashtable]) {
            ForEach ($Key in $h.Keys) {$Out.$Key = If ($Out.ContainsKey($Key)) {@($Out.$Key) + $h.$Key} Else {$h.$Key}}
        }
    }
    If ($Operator) {ForEach ($Key in @($Out.Keys)) {$_ = @($Out.$Key); $Out.$Key = Invoke-Command $Operator}}
    $Out
}

$hash1, $hash2 | CombineHash

This particular example creates a function named CombineHash that accepts a list of hash tables piped into it and combines the hash tables into a single hash table.

If there are duplicate keys between the hash tables, a single key will be shown in the combined hash table and the values will be added to an array.

Using this function, you can also use the $Operator parameter of the function to specify how to handle values from duplicate keys.

The following example shows how to use each method in practice.

Example 1: Combine Hash Tables (With No Duplicate Keys)

We can use the following code to combine two hash tables named $hash1 and $hash2 that contain no duplicate keys:

$hash1 = @{A=5; B=10; C=15}
$hash2 = @{D=20; E=25; F=30}

$new_hash = $hash1 + $hash2

The following screenshot shows how to use this syntax in practice:

PowerShell combine hash tables

The result is a single hash table named $new_hash that combines the values from both $hash1 and $hash2.

Example 2: Combine Hash Tables (With Duplicate Keys)

We can create the following function named CombineHash to combine hash tables together that may have duplicate keys:

Function CombineHash([ScriptBlock]$Operator) {
    $Out = @{}
    ForEach ($h in $Input) {
        If ($h -is [Hashtable]) {
            ForEach ($Key in $h.Keys) {$Out.$Key = If ($Out.ContainsKey($Key)) {@($Out.$Key) + $h.$Key} Else {$h.$Key}}
        }
    }
    If ($Operator) {ForEach ($Key in @($Out.Keys)) {$_ = @($Out.$Key); $Out.$Key = Invoke-Command $Operator}}
    $Out
}

We can then use the following code to create two hash tables that share some duplicate keys and use the CombineHash function to combine them into a single hash table:

$hash1 = @{A=5; B=10; C=15}
$hash2 = @{A=20; B=25; F=30}

$hash1, $hash2 | CombineHash

The following screenshot shows how to use this syntax in practice:

PowerShell combine hash tables

The combined hash table contains the values from both original hash tables, with values that share a key grouped into an array.

You could also use the following syntax to simply return the average value of duplicate keys:

$hash1 = @{A=5; B=10; C=15}
$hash2 = @{A=20; B=25; F=30}

$hash1, $hash2 | CombineHash {($_ | Measure-Object -Average).Average}

The following screenshot shows how to use this syntax in practice:

PowerShell combine hash tables and return average of duplicate keys

The value shown in the Value column now represents the average value between the duplicate keys.

Feel free to replace Average with Sum, Maximum, Minimum, etc. to return a different aggregate value instead.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Check if Hash Table Contains Specific Key
PowerShell: How to Remove Elements from a Hash Table
PowerShell: How to Iterate Over a Hash Table
PowerShell: How to Update Values in Hash Table

Leave a Reply