How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

user4317867 picture user4317867 · Jun 5, 2016 · Viewed 52.7k times · Source

Editor's note: This question has a complicated history, but boils down to this:
* To learn how to enumerate the entries of a hashtable by its key-value pairs, see the accepted answer.
* To learn how to filter a hashtable by a collection of key values, see the other answer.


I think I fell into the X Y problem again, my initial question was about Filtering a hash table. I discovered it's easier to filter before creating the hash table. Question answered, right?

Nope, the Y problem was looping each Key and using the Values which @briantist helped me with.

My goal is to loop over the key names, which are timestamps, and schedule a task using the key name as the task name and trigger.

I'm creating a hash table from a CSV file using Group-Object -AsHashTable -AsString -edit, it's worth mentioning here that filtering the CSV before creating the HashTable only makes things easier down the Pipeline or script.

As an example:

Import-CSV (ls -path D:\ -Filter source*.csv | sort LastWriteTime | Select -Last 1).FullName |
 where {$_.TimeCorrected -ne 'ManualRebootServer'} |
 group TimeCorrected -AsHashTable -AsString

I'm trying to loop over the key names and able to display the key names using:

$var = Import-Csv csv123.csv | Group-Object Value1 -AsHashTable -AsString

foreach ($key in $var.Keys){"The key name is $key"}

#Create a scheduled task named and triggered based on the HashTable keyname
#test test test
foreach ($key in $var.keys){IF($key -ne 'ManualRebootServer'){"Register-ScheduledJob"}}

I'm just not sure how to get the values from the keys I am interested in.

I've found the following works, but only when I enter a Key name manually. I'm just unsure how to combine both loops.

($val.GetEnumerator() | Where {$_.key -eq '06-11-16 18:00'} | ForEach-Object { $_.value }).Server

Answer

briantist picture briantist · Jun 5, 2016

You have some options here.

Enumerating through keys:

foreach ($key in $var.Keys) {
    $value = $var[$key]
    # or
    $value = $var.$key 
}

Enumerating key-value pairs (which you've discovered, but may not be using effectively):

foreach ($kvp in $var.GetEnumerator()) {
    $key = $kvp.Key
    $val = $kvp.Value
}