- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
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 = @( 'Corn', 'Beef', 'Green Beans', 'Carrots', '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 { $_ }
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.
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.
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.
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} Corn Carrots Beef Green Beans Brussel Sprouts PS C:\> $foods.GetEnumerator() | ForEach-Object {$_.Value} Yellow Orange Brown Green Green
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.
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.