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
Future Of The Big Data Job Market Post-Pandemic | Executive Levels
Ledger | Meaning, Format, Example and Balancing of Accounts - GeeksforGeeks
[PDF] (punctuation mark - used as punctuation in symbol sentences) YELLOW Character 8485 Fragezeichen 8485 vraagteken 8485 vraagteken - Free Download PDF
Metro By T Mobile Sign In
Att U Verse Outage Map
Wavmonopoly Reverb Calculator
Pleads Irksomely Crossword Clue
Unblocked Games6969: A World Of Unrestricted Gaming Fun - Unblocked Hub
Pharmacies in Amsterdam (Apotheek) | Amsterdam.info
Nlxf North Liberty
A Killer Paradox: how to watch, plot, cast and everything we know
Amrn Investors Hub
Best Restaurants Ventnor
Dekalb County Jail Fort Payne Alabama
Craigslist Cars Lansing Michigan
Craigslist Artesia Nm
Weapons Storehouse Nyt Crossword
Rubfinder
Express Employment Sign In
4 Pm Edt
Just Breath Chords
Legitlocal.co Lawn Service Near Me
412-690-0001
Jobs Near Me 80K
¿Cuándo se regalan flores amarillas y por qué se realiza este ritual en septiembre?
Splunk If Command
Paperlesspay Talx Ingram
Mytowerlearninghub
Ellie Zeiler Ass
Erfahrungen mit r2 Bike
Chubby Mature Bbc
Newadvent Org
Costco Gas Kingman Az
Devotion Showtimes Near Cinemark Sherman
Les 4 meilleures cartes SIM prépayées (2024) - NON sponsorisé
Camwhor*s Bypass 2022
Lions Roster Wiki
Amari Cooper Pfr
Walmart On Wetmore
Gypsy Rose Blanchard's Mother's Brutal Crime Scene Photos Go Viral On Her 33rd Birthday
Atrium Orthopedic Urgent Care
Wral Nighttime Lottery
Honeybee: Classification, Morphology, Types, and Lifecycle
Boise Cascade Aktie (BCC) • US09739D1000
7440 Dean Martin Dr Suite 204 Directions
Ixl Mililani High School
What is "Brrr skibidi dop dop / dom dom yes yes"? Memes, explanation, meaning, definition - Bedeutung Online
Madelyn Cline Surgeon
Nearest O'reillys To Me
Salmon Fest 2023 Lineup
Cta Bus Tracker 77
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.