PowerShell Hashtable show first key

Mik*_*ike 6 powershell hashtable

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'}
Run Code Online (Sandbox Code Playgroud)

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

Oli*_*ver 7

您可以访问哈希表中的 Key/ValueCollection:

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

将返回

Monday
Dienstag
Run Code Online (Sandbox Code Playgroud)

在 $() 中包含对“Keys”的调用将导致集合被转换为对象数组,如下所示:

$Weekdays = @{Monday = 'Montag';Tuesday = 'Dienstag'}
$Weekdays.Keys.Gettype()
$($Weekdays.Keys).Gettype()
Run Code Online (Sandbox Code Playgroud)

这使

IsPublic IsSerial Name                                     BaseType                                                                                                                     
-------- -------- ----                                     --------                                                                                                                     
False    True     KeyCollection                            System.Object                                                                                                                
True     True     Object[]                                 System.Array  
Run Code Online (Sandbox Code Playgroud)

并且可以使用整数索引对象数组。