Enumerating a hashtable

A hashtable can return the information it holds in several ways. Start with the hashtable:

$hashtable = @{ 
    Key1 = 'Value1' 
    Key2 = 'Value2' 
} 

Keys can be returned using the Keys property of the hashtable, which returns KeyCollection:

$hashtable.Keys 

Values can be returned using the Values property, which returns ValueCollection. The key is discarded when using the Values property:

$hashtable.Values 

A simple loop can be used to retain the association between key and value:

foreach ($key in $hashtable.Keys) { 
    Write-Host "Key: $key    Value: $($hashtable[$key])" 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.246.203