Collections:
Other Resources:
Loop Hashtable Members in Windows PowerShell Scripts
How to use Foreach loops in Windows PowerShell scripts?
✍: FYIcenter.com
There are a couple of ways of loop through members of a hashtable.
1. Using "Keys" property - The "Keys" property holds a Collection object of all keys in a hashtable. The Collection object of keys can be used in a "Foreach {...}" loop as shown below:
$p = @{FirstName="John";LastName="Smith";Age=40}
"My profile:"
Foreach ($k in $p.Keys) {
" "+$k+": "+$p.$k
}
2. Using "Values" property - The "Values" property holds a Collection object of all values in a hashtable. The Collection object of keys can be used in a "Foreach {...}" loop.
$p = @{FirstName="John";LastName="Smith";Age=40}
"My profile:"
Foreach ($v in $p.Values) {
" "+$v
}
3. Using "GetEnumerator()" method - The "GetEnumerator()" method returns a Collection of DictionaryEntry objects represents all members in a hashtable. The Collection can be used in a "Foreach {...}" loop to loop through each DictionaryEntry with its "Key" and "Value" properties:
$p = @{FirstName="John";LastName="Smith";Age=40}
"My profile:"
Foreach ($e in $p.GetEnumerator()) {
" "+$e.Key+": "+$e.Value
}
⇒ Execution Status and Errors in Windows PowerShell Scripts
⇐ Hashtable in Windows PowerShell Scripts
2016-10-29, ∼4021🔥, 0💬
Popular Posts:
Can I disable Windows service "Portable Media Serial Number" to speedup my computer? Windows service...
What is "Ulead Burning Helper" in my Windows 7 service list? And how is "Ulead Burning Helper" servi...
What is service "AEADIFilters" and program "AEADISRV.EXE" on Windows Vista? "AEADIFilters" is a syst...
Whenever I try to open the mail app I get a white screen with a grey banner that says Mail Can't Ope...
What is yt.dll - DLL File - Yahoo Toolbar for IE Browser? yt.dll is installed as part of the Yahoo! ...