PowerShell Hashtable show first key
Asked Answered
M

2

9

I am playing around with hashtables in powershell and try to figure out, if there is a way, to show the content of Key1 (not the value).

I tried several ways to have as an result "Monday" but either I get all the names of the table, a blank return or an error message.

here's my table:

$Weekdays = @{Monday = 'Montag';Tuesday = 'Dienstag'}

If possible, I would like to have as an output only "Monday", is there a way, I can enter code to have "Monday" as an output?

Thank you very much for your help,

Mike

Marienthal answered 21/8, 2019 at 15:0 Comment(1)
do you mean ($Weekdays).ValuesDaemon
W
14

You can access the Key/ValueCollection inside the hashtable:

$Weekdays = @{Monday = 'Montag';Tuesday = 'Dienstag'}    
echo $($Weekdays.Keys)[0]
echo $($Weekdays.Values)[1]

will return

Monday
Dienstag

enclosing the call to "Keys" in $() will result in the Collection being converted to an Object Array, as you ca see here:

$Weekdays = @{Monday = 'Montag';Tuesday = 'Dienstag'}
$Weekdays.Keys.Gettype()
$($Weekdays.Keys).Gettype()

which gives

IsPublic IsSerial Name                                     BaseType                                                                                                                     
-------- -------- ----                                     --------                                                                                                                     
False    True     KeyCollection                            System.Object                                                                                                                
True     True     Object[]                                 System.Array  

and an object array can be indexed into with integers.

Willful answered 21/8, 2019 at 15:53 Comment(4)
Hello Oliver, Thanx a lot, that is what I was searching and that will help me a lot. You saved me from a headache, :) Thank youMarienthal
another thing to add to the list of things I hate about powershell. Can you explain what $(...) exactly?Tarsal
A few things wrong here: The keys inside a hashtable are not guaranteed to be returned in the same order they were defined in unless you use an ordered dictionary: $Weekdays = [ordered] @{...etc...} Secondly, $($Weekdays.Keys)[0] will only get the first key if you have more than one item in your table. If just the Monday value exists, Powershell turns your array into a scalar and [0] just gets the first character "M"Rosiorosita
P.S. you'd actually want @($Weekdays.Keys)[0] because this ensures that the keys are converted to a list, even if there's only one of them.Rosiorosita
G
1

$Weekdays = @{Monday = 'Montag';Tuesday = 'Dienstag'}

$Weekdays["Monday"] will print out Montag. it is like Arry with index. In Hashtable/Dictionary the key is the index. Definitely remember to put the key in quote. Otherwise it will fail.

Gowan answered 11/8, 2021 at 6:43 Comment(1)
Thanx a lot for explaining Abul AhmedMarienthal

© 2022 - 2024 — McMap. All rights reserved.