Looping through a hash, or using an array in PowerShell Looping through a hash, or using an array in PowerShell powershell powershell

Looping through a hash, or using an array in PowerShell


Shorthand is not preferred for scripts; it is less readable. The %{} operator is considered shorthand. Here's how it should be done in a script for readability and reusability:

Variable Setup

PS> $hash = @{    a = 1    b = 2    c = 3}PS> $hashName                           Value----                           -----c                              3b                              2a                              1

Option 1: GetEnumerator()

Note: personal preference; syntax is easier to read

The GetEnumerator() method would be done as shown:

foreach ($h in $hash.GetEnumerator()) {    Write-Host "$($h.Name): $($h.Value)"}

Output:

c: 3b: 2a: 1

Option 2: Keys

The Keys method would be done as shown:

foreach ($h in $hash.Keys) {    Write-Host "${h}: $($hash.Item($h))"}

Output:

c: 3b: 2a: 1

Additional information

Be careful sorting your hashtable...

Sort-Object may change it to an array:

PS> $hash.GetType()IsPublic IsSerial Name                                     BaseType-------- -------- ----                                     --------True     True     Hashtable                                System.ObjectPS> $hash = $hash.GetEnumerator() | Sort-Object NamePS> $hash.GetType()IsPublic IsSerial Name                                     BaseType-------- -------- ----                                     --------True     True     Object[]                                 System.Array

This and other PowerShell looping are available on my blog.


Christian's answer works well and shows how you can loop through each hash table item using the GetEnumerator method. You can also loop through using the keys property. Here is an example how:

$hash = @{    a = 1    b = 2    c = 3}$hash.Keys | % { "key = $_ , value = " + $hash.Item($_) }

Output:

key = c , value = 3key = a , value = 1key = b , value = 2


You can also do this without a variable

@{  'foo' = 222  'bar' = 333  'baz' = 444  'qux' = 555} | % getEnumerator | % {  $_.key  $_.value}