PowerShell hash tables are versatile constructs. Scripts use hash tables all the time to store key/value pairs. One common problem a lot of PowerShell newcomers struggle with is reading all items in a hash table with a loop. They think a hash table is similar to an array, but this is not the case.

For example, when reading all items in an array, we can use a simple foreach loop. To demonstrate, let's say I've an array of five foods. I'd like to output each of these foods to the console.

$foods = @(
    'Green Beans',
    'Brussel Sprouts'

$foods | ForEach-Object { $_ }

Executing this short snippet would return each of the foods to the console. Iterating over elements in an array like this is extremely common. Newcomers not familiar with how a hash table works may initially try to do the same and will see a different kind of output.

Perhaps I have another hash table and I try the same approach.

$foods = @{
    'Corn' = 'Yellow'
    'Beef' = 'Brown'
    'Green Beans' = 'Green'
    'Carrots' = 'Orange'
    'Brussel Sprouts' = 'Green'

$foods | ForEach-Object { $_ }
Viewing name value pairs in a hash table

Viewing name value pairs in a hash table

You can see above that we get two columns: a name and a value. Perhaps I'd like to see only the keys. To do this, I typically can reference the property names, which I believe are Key and Value.

Error message when referencing Key

Error message when referencing Key

Notice above that I receive an error message. PowerShell doesn't know about this key property. This is because the hash table isn't enumerable. We can iterate over each key/value pair in a hash table a couple of different ways. First, we could use the Keys property on the hash table, which contains all of the keys in order.

Keys and Values properties

Keys and Values properties

You can see above that I'm able to reference either all of the keys or all of the values. I could also use a foreach loop to pass key names to the hash table and output values as well.

Referencing the Keys property

Referencing the Keys property

Like with everything in PowerShell, there's more than one way to do this. My personal, favorite way is to use the GetEnumerator() method. I believe it's simpler than looping over each string in the Keys property. By using the GetEnumerator() method, we can essentially "convert" the hash table into an array of objects, which allows us to reference the Key and Value properties like we tried to do in the beginning.

To use the GetEnumerator() method, simply append it to the end of the hash table. When used by itself, you can't tell the difference between using it or not.

PS C:\> $foods.GetEnumerator()

Name                           Value
----                           -----
Corn                           Yellow
Carrots                        Orange
Beef                           Brown
Green Beans                    Green
Brussel Sprouts                Green

However, the output is different. At this point, we can now directly reference the Key and Value properties without receiving the same error we did before.

PS C:\> $foods.GetEnumerator() | ForEach-Object {$_.Key}
Green Beans
Brussel Sprouts
PS C:\> $foods.GetEnumerator() | ForEach-Object {$_.Value}

From here, we can then reference the Key and Value properties however we'd like. Perhaps we'd like to concatenate them. We can now easily do this:

Subscribe to 4sysops newsletter!

PS C:\> $foods.GetEnumerator() | ForEach-Object {"$($_.Key) - $($_.Value)"}
Corn - Yellow
Carrots - Orange
Beef - Brown

By being able to reference the key and value separately, we can use them as normal object properties.

1 Comment
  1. Lars Panzerbjørn 4 years ago

    I just wanted to say thanks for this.

    I’d been struggling with AWS S3 Tag Sets (Get-S3ObjectTagset), as they don’t behave like key/value pairs should, and the GetEnumerator allowed me to beat them into submission.

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account