Question:
Given:
1 2 3 4 5 |
$settings = @{"Env1" = "VarValue1"; "Env2" = "VarValue2" } Write-Output "Count: $($settings.Values.Count)" Write-Output "Value 0: '$($settings.Values[0])'" Write-Output "Value 1: '$($settings.Values[1])'" |
I get the output:
1 2 3 4 |
Count: 2 Value 0 : 'VarValue2 VarValue1' Value 1 : '' |
Why does the first element have both values and the second have none? How do I get the values as a collection I can index?
Answer:
I figured it out. The solution is to convert the Values ICollection to an array of strings.
With:
1 2 |
$values = $settings.Values -as [string[]] |
The output becomes as originally expected:
1 2 3 4 |
Count: 2 Value 0 : 'VarValue2' Value 1 : 'VarValue1' |
I cannot help but feel that this should be the default behaviour.