- 1 year ago
Iterate through Hashtable containing List of values in Powershell
Comment
1 / 1
To iterate through a Hashtable containing a list of values in PowerShell, you can use a combination of nested foreach loops. Here's an example:
powershell$hashtable = @{
Key1 = @(1, 2, 3)
Key2 = @(4, 5, 6)
Key3 = @(7, 8, 9)
}
foreach ($key in $hashtable.Keys) {
Write-Host "Key: $key"
foreach ($value in $hashtable[$key]) {
Write-Host "Value: $value"
}
Write-Host "---"
}
In this example, we have a Hashtable named $hashtable
with keys (Key1
, Key2
, Key3
) and corresponding lists of values. The outer foreach loop iterates over each key in the Hashtable, and for each key, the inner foreach loop iterates over the corresponding list of values.
You can replace the Write-Host
statements with your desired logic or actions to perform on each key-value pair.
The output of the above code will be:
yamlKey: Key1
Value: 1
Value: 2
Value: 3
---
Key: Key2
Value: 4
Value: 5
Value: 6
---
Key: Key3
Value: 7
Value: 8
Value: 9
---
Feel free to modify the code based on your specific requirements and the structure of your Hashtable.