Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing into a PowerShell value collection

Given:

$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:

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?

like image 202
Swoogan Avatar asked Feb 11 '23 20:02

Swoogan


1 Answers

I figured it out. The solution is to convert the Values ICollection to an array of strings.

With:

$values = $settings.Values -as [string[]]

The output becomes as originally expected:

Count: 2
Value 0 : 'VarValue2'
Value 1 : 'VarValue1'

I cannot help but feel that this should be the default behaviour.

like image 193
Swoogan Avatar answered Feb 21 '23 22:02

Swoogan