Enumerating a dictionary

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

$dictionary = New-Object System.Collections.Generic.Dictionary"[String,IPAddress]" 
$dictionary.Add("Computer1", "192.168.10.222")   
$dictionary.Add("Computer2", "192.168.10.13") 

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

$dictionary.Keys 

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

$dictionary.Values 

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

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

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