Working with Hash Tables (Dictionaries) in PowerShell
📖 Scenario: You are managing a small store's inventory. You want to keep track of product names and their quantities using a hash table in PowerShell.
🎯 Goal: Build a PowerShell script that creates a hash table for products and quantities, sets a minimum stock threshold, filters products below that threshold, and displays those products.
📋 What You'll Learn
Create a hash table named inventory with exact product names and quantities
Create a variable named minStock with a numeric value
Use a foreach loop to find products with quantity less than minStock
Store filtered products in a new hash table named lowStock
Print the lowStock hash table at the end
💡 Why This Matters
🌍 Real World
Managing inventory levels in small businesses or stores to know when to reorder products.
💼 Career
PowerShell scripting for system administrators or IT professionals automating inventory or configuration tasks.
Progress0 / 4 steps
1
Create the inventory hash table
Create a hash table called inventory with these exact entries: 'Apples' = 30, 'Bananas' = 12, 'Oranges' = 20, 'Grapes' = 5
PowerShell
Hint
Use @{} to create a hash table in PowerShell. Separate entries with semicolons.
2
Set the minimum stock threshold
Create a variable called minStock and set it to 15
PowerShell
Hint
Just assign the number 15 to the variable minStock.
3
Find products with low stock
Create an empty hash table called lowStock. Use a foreach loop with variables product and quantity to iterate over $inventory.GetEnumerator(). Inside the loop, if quantity is less than minStock, add the product and quantity to lowStock.
PowerShell
Hint
Use $inventory.GetEnumerator() to loop through the hash table. Use $lowStock[$product] = $quantity to add entries.
4
Display the low stock products
Print the lowStock hash table using Write-Output
PowerShell
Hint
Use Write-Output $lowStock to display the hash table.
Practice
(1/5)
1. What is the correct way to create an empty hash table in PowerShell?
easy
A. ()
B. []
C. @{}
D. {}
Solution
Step 1: Understand hash table syntax in PowerShell
PowerShell uses @{} to define hash tables, which store key-value pairs.
Step 2: Identify the empty hash table syntax
An empty hash table has no key-value pairs inside, so it is @{}.
Final Answer:
@{} -> Option C
Quick Check:
Empty hash table = @{ } [OK]
Hint: Use @{} to create hash tables in PowerShell [OK]
Common Mistakes:
Using {} which is a script block, not a hash table
Using [] which is an array, not a hash table
Using () which is for grouping or method calls
2. Which of the following is the correct syntax to add a key-value pair 'Name' = 'Alice' to an existing hash table $person?
easy
A. $person[Name] = 'Alice'
B. $person.Name = Alice
C. $person->Add('Name', 'Alice')
D. $person.Add('Name', 'Alice')
Solution
Step 1: Recall how to add key-value pairs to hash tables
PowerShell hash tables have an Add() method to add new keys and values.
Step 2: Identify correct method call syntax
The correct syntax is $person.Add('Name', 'Alice'). Note that the Add() method specifically adds new entries.
Final Answer:
$person.Add('Name', 'Alice') -> Option D
Quick Check:
Add key-value with Add() method [OK]
Hint: Use Add() method to add new keys to hash tables [OK]
Common Mistakes:
Using -> which is not PowerShell syntax
Using dot notation with unquoted value
Using unquoted key in square brackets
3. What will be the output of this PowerShell code?
D. $userAges = @{}; for ($i=0; $i -le $users.Count; $i++) { $userAges[$i] = $ages[$i] }
Solution
Step 1: Understand the goal
Create a hash table where each user name is a key and their corresponding age is the value.
Step 2: Analyze each option
$userAges = @{}; for ($i=0; $i -lt $users.Count; $i++) { $userAges[$users[$i]] = $ages[$i] } uses a for loop with index to assign each user key to the correct age value. $userAges = @{}; foreach ($user in $users) { $userAges.Add($user, $ages) } tries to add all ages at once, which is incorrect. $userAges = @{}; $users | ForEach-Object { $userAges.Add($_, $ages[$_]) } uses $_ as index but $_ is the user string, not an index. $userAges = @{}; for ($i=0; $i -le $users.Count; $i++) { $userAges[$i] = $ages[$i] } loops one too many times and uses numeric keys instead of user names.
Final Answer:
$userAges = @{}; for ($i=0; $i -lt $users.Count; $i++) { $userAges[$users[$i]] = $ages[$i] } -> Option B
Quick Check:
Use indexed loop to map keys to values [OK]
Hint: Use index loop to map two lists into hash table [OK]
Common Mistakes:
Using foreach without index to access parallel array
Using wrong loop bounds causing errors
Assigning numeric keys instead of user names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.