A hash table is a collection of key-value pairs. It lets you store and quickly find data by using a unique key, like a labeled box for your information.
Click to reveal answer
beginner
How do you create an empty hash table in PowerShell?
You create an empty hash table using @{} like this: $myHash = @{}
Click to reveal answer
beginner
How do you add a key-value pair to a hash table?
Use the syntax: $hashTable["key"] = "value" This adds or updates the value for the given key.
Click to reveal answer
beginner
How do you access a value from a hash table by its key?
Use the key inside square brackets: $value = $hashTable["key"] This gets the value stored for that key.
Click to reveal answer
beginner
What happens if you try to access a key that does not exist in a hash table?
PowerShell returns nothing (null). It does not cause an error, but you get no value back.
Click to reveal answer
Which symbol is used to create a hash table in PowerShell?
A()
B[]
C{}
D@{}
✗ Incorrect
The @{} syntax creates a hash table in PowerShell.
How do you add a new key 'Name' with value 'Alice' to a hash table $h?
A$h.Add('Name', 'Alice')
B$h['Name'] = 'Alice'
C$h.Name = 'Alice'
D$h->Name = 'Alice'
✗ Incorrect
In PowerShell, you add or update a key-value pair using $h['Name'] = 'Alice'.
What does $h['Age'] return if 'Age' is not a key in $h?
ANull (no value)
BAn error
C0
DThe string 'Age'
✗ Incorrect
Accessing a missing key returns null, not an error.
How do you create a hash table with keys 'City' and 'Country' and values 'Paris' and 'France'?
A$h = @{City='Paris'; Country='France'}
B$h = ['City'='Paris', 'Country'='France']
C$h = {'City':'Paris', 'Country':'France'}
D$h = (City='Paris', Country='France')
✗ Incorrect
The correct syntax uses @{} with semicolons separating key-value pairs.
Which of these is NOT a valid way to access a hash table value?
A$h['Key']
B$h.Key
C$h->Key
D$h.Get_Item('Key')
✗ Incorrect
PowerShell does not use '->' to access hash table items.
Explain how to create, add to, and retrieve data from a hash table in PowerShell.
Think of a hash table as a labeled box where you put and get items by their labels.
You got /3 concepts.
What happens when you try to get a value from a hash table using a key that does not exist? How can this behavior be useful?
Consider how you might safely check for data without breaking your script.
You got /3 concepts.
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.