0
0
PowerShellscripting~5 mins

Hash tables (dictionaries) in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a hash table in PowerShell?
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: <br>$myHash = @{}
Click to reveal answer
beginner
How do you add a key-value pair to a hash table?
Use the syntax: <br>$hashTable["key"] = "value"<br>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: <br>$value = $hashTable["key"]<br>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@{}
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'
What does $h['Age'] return if 'Age' is not a key in $h?
ANull (no value)
BAn error
C0
DThe string 'Age'
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')
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')
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.