Private/Join-Hashtable.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function Join-Hashtable {
    <#
 .SYNOPSIS
  Combines multiple hashtables into a single table.
 
 .DESCRIPTION
  Combines multiple hashtables into a single table.
  On multiple identic keys, the last wins.
 
 .EXAMPLE
  PS C:\> Join-Hashtable -Hashtable $Hash1, $Hash2
 
  Merges the hashtables contained in $Hash1 and $Hash2 into a single hashtable.
#>

    [CmdletBinding()]
    Param (
        # The tables to merge.
        [Parameter( Mandatory, ValueFromPipeline )]
        [AllowNull()]
        [System.Collections.IDictionary[]]
        $Hashtable
    )
    begin {
        $table = @{ }
    }

    process {
        foreach ($item in $Hashtable) {
            foreach ($key in $item.Keys) {
                $table[$key] = $item[$key]
            }
        }
    }

    end {
        $table
    }
}