0
0
PowerShellscripting~10 mins

Hash tables (dictionaries) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hash tables (dictionaries)
Create empty hash table
Add key-value pairs
Access value by key
Update or remove pairs
Use hash table for fast lookup
End
This flow shows how a hash table is created, filled with key-value pairs, accessed, updated, and used for quick data lookup.
Execution Sample
PowerShell
$hash = @{}
$hash['name'] = 'Alice'
$hash['age'] = 30
$name = $hash['name']
$hash['age'] = 31
$hash.Remove('name')
$hash
This code creates a hash table, adds keys 'name' and 'age', updates 'age', removes 'name', and then outputs the hash table.
Execution Table
StepActionHash Table StateOutput
1Create empty hash table{}
2Add key 'name' with value 'Alice'{'name': 'Alice'}
3Add key 'age' with value 30{'name': 'Alice', 'age': 30}
4Access value for key 'name'{'name': 'Alice', 'age': 30}Alice
5Update key 'age' to 31{'name': 'Alice', 'age': 31}
6Remove key 'name'{'age': 31}
7Output final hash table{'age': 31}@{age=31}
💡 All operations complete, final hash table contains only 'age' with value 31.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
$hash{}{}{'name': 'Alice'}{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 31}{'age': 31}{'age': 31}
$nameundefinedundefinedundefinedAliceAliceAliceAliceAlice
Key Moments - 3 Insights
Why does accessing $hash['name'] after removal still show 'Alice' in the variable tracker?
Because $name was assigned the value 'Alice' before the key 'name' was removed from the hash table (see step 4 in execution_table). Removing the key later does not change the already stored variable.
What happens if you try to access a key that does not exist in the hash table?
PowerShell returns null or nothing for a missing key. This is not shown in the current table but is important to remember when accessing keys.
Why is the hash table output shown as '@{age=31}' instead of a JSON-like string?
PowerShell displays hash tables in its own format '@{key=value}'. This is normal and means the hash table contains the key 'age' with value 31.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $hash after step 3?
A{'age': 31}
B{'name': 'Alice'}
C{'name': 'Alice', 'age': 30}
D{}
💡 Hint
Check the 'Hash Table State' column at step 3 in the execution_table.
At which step is the key 'name' removed from the hash table?
AStep 6
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the 'Remove key' action in the execution_table.
If you tried to access $hash['name'] after step 6, what would happen?
AIt would return 'Alice'
BIt would return null or nothing
CIt would return 31
DIt would cause an error
💡 Hint
Consider what happens when a key is removed from a hash table (see key_moments).
Concept Snapshot
Hash tables store data as key-value pairs.
Create with @{} and add pairs like $hash['key'] = value.
Access values by keys: $hash['key'].
Update by assigning new value to existing key.
Remove keys with $hash.Remove('key').
Fast lookup by keys is the main benefit.
Full Transcript
This lesson shows how to use hash tables (dictionaries) in PowerShell. We start by creating an empty hash table with @{}. Then we add key-value pairs like 'name' and 'age'. We access values by their keys, update values, and remove keys. The execution table traces each step, showing how the hash table changes. Variables like $hash and $name are tracked to see their values over time. Key moments clarify common confusions, such as why a variable keeps a value even after the key is removed. The visual quiz tests understanding of the hash table state at different steps. Finally, the concept snapshot summarizes the main points for quick review.