Getting the current hash key in a ForEach-Object loop in powershell

前端 未结 3 1272
生来不讨喜
生来不讨喜 2021-02-07 10:37

I\'ve got a hash table:

$myHash = @{ 
   \"key1\" = @{
       \"Entry 1\" = \"one\"
       \"Entry 2\" = \"two\"
   }
            


        
3条回答
  •  情书的邮戳
    2021-02-07 10:52

    I like to use GetEnumerator() when looping a hashtable. It will give you a property value with the object, and a property key with it's key/name. Try:

    $myHash.GetEnumerator() | % { 
        Write-Host "Current hashtable is: $($_.key)"
        Write-Host "Value of Entry 1 is: $($_.value["Entry 1"])" 
    }
    

提交回复
热议问题