Iterate Over A Hashtable in PowerShell (2024)

Iterating over an array in PowerShell using a foreach loop is pretty simple. You might think you can do the same thing with a hash table using the same syntax, but if you do you’ll get nothing back.

It is possible to loop over a hash table though, using one of two methods.

First, let’s create a simple hashtable.

$hash = @{'About Arcane Code' = 'http://arcanecode.me''ArcaneCode Blog' = 'https://arcanecode.com''ArcaneCode RedGate Articles' = 'http://arcanecode.red''ArcaneCode Github Repository' = 'http://arcanerepo.com'}

In the first method, the one that I prefer, you can use the GetEnumerator method of the hash table object.

foreach ($h in $hash.GetEnumerator() ){ Write-Host "$($h.Name) : $($h.Value)"}

Within the loop, you can use the Name property to get the key part of the hash, and the Value property to retrieve the value. Here is the output:

ArcaneCode Blog : https://arcanecode.com ArcaneCode Github Repository : http://arcanerepo.com About Arcane Code : http://arcanecode.me ArcaneCode RedGate Articles : http://arcanecode.red

In the second method, instead of iterating over the hash table itself, we loop over the Keys of the hash table.

foreach ($h in $hash.Keys) { Write-Host "$h: $($hash.Item($h))"}

For each key, we retrieve it’s value from the hash table using the key to indicate the item we want. We could have shortened this slightly, but skipping the Item and just referencing the value by the key, using this syntax:

foreach ($h in $hash.Keys) { Write-Host "$h: $($hash.$h)"}

Both of these methods produce the same output as our original version.

ArcaneCode Blog : https://arcanecode.com ArcaneCode Github Repository : http://arcanerepo.com About Arcane Code : http://arcanecode.me ArcaneCode RedGate Articles : http://arcanecode.red

There you go, two simple ways in which you can iterate over a hash table in PowerShell. As I indicated, I prefer GetEnumerator because I have access to both the key and the value in a single variable within my loop. But feel free to use the method that works best for your situation.

Iterate Over A Hashtable in PowerShell (1)

Published by arcanecode

View all posts by arcanecode

Published

Iterate Over A Hashtable in PowerShell (2024)
Top Articles
Get Your Google Sheets API Key | Hands-On Data Visualization
“Poor dad” vs "Rich Dad" Mentality
Mickey Moniak Walk Up Song
DPhil Research - List of thesis titles
Pnct Terminal Camera
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Fort Carson Cif Phone Number
Fully Enclosed IP20 Interface Modules To Ensure Safety In Industrial Environment
Craigslist Free Stuff Appleton Wisconsin
Seething Storm 5E
7543460065
Hover Racer Drive Watchdocumentaries
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
Capitulo 2B Answers Page 40
Nonuclub
Cooking Fever Wiki
The Cure Average Setlist
Urban Dictionary: hungolomghononoloughongous
Who called you from +19192464227 (9192464227): 5 reviews
Walgreens San Pedro And Hildebrand
Pretend Newlyweds Nikubou Maranoshin
Melendez Imports Menu
Yog-Sothoth
What Is The Lineup For Nascar Race Today
Bill Remini Obituary
Parkeren Emmen | Reserveren vanaf €9,25 per dag | Q-Park
Fiona Shaw on Ireland: ‘It is one of the most successful countries in the world. It wasn’t when I left it’
Kabob-House-Spokane Photos
Acurafinancialservices Com Home Page
Craigslist Rentals Coquille Oregon
Umn Biology
Toonkor211
Hannah Jewell
A Plus Nails Stewartville Mn
Halsted Bus Tracker
Workboy Kennel
Synchrony Manage Account
Sephora Planet Hollywood
Wo ein Pfand ist, ist auch Einweg
Nid Lcms
Levi Ackerman Tattoo Ideas
Senior Houses For Sale Near Me
Booknet.com Contract Marriage 2
bot .com Project by super soph
Quest Diagnostics Mt Morris Appointment
Wera13X
Razor Edge Gotti Pitbull Price
Wvu Workday
Craigslist.raleigh
Thrift Stores In Burlingame Ca
E. 81 St. Deli Menu
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5933

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.