Reputation: 5558
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?
Upvotes: 4
Views: 5246
Reputation: 1
Based on your $settings object, you can simply use » ($settings).Values
Example:
($settings).Values | % { '• ' + $_ }
Returns:
• VarValue2
• VarValue1
Upvotes: 0
Reputation: 5558
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.
Upvotes: 7
Reputation: 1772
You can't directly index a hashtable like that. Gotta do this
($settings.GetEnumerator() | select -expand value)[0]
From help about_hash_tables
Hash table tables are not arrays, so you cannot use an integer as an index into the hash table, but you can use a key name to index into the hash table. If the key is a string value, enclose the key name in quotation marks.
Upvotes: 0