What is a Dictionary in PowerShell?
Looking around a Dictionary is a Data Structure in which you can store key and value pairs. In PowerShell it is more common to use hashtables. Some common uses for hashtables would be: storing IP addresses with computer names. If I was an Admin I would store MAC addresses with computer names, lots of uses for it.
In PowerShell, each hash table is a Hashtable (System.Collections.Hashtable) object. You can use the properties and methods of Hashtable objects in PowerShell.
The keys and value in hash tables are also .NET objects. They are most often strings or integers, but they can have any object type.
To create an empty hash table type:
$hash = @{}
to create a hashtable with Keys and values you would type something like:
$hash = @{PuertoRico = "San Juan" ; Army = "The Best" ; Motivation = " Success" }
or Here is another example:
$hashtable = @{"Key1" = "ValueforKey1"; "Key2" = "ValueforKey2"; "Key3" = "ValueforKey3"}
You can also create an Ordered Hash table by adding the Object type OrderedDictionary in the front of the hash table example below:
TO understand the differences between ordered and regular hash tables is better to actually type the hash then get the keys or values.
$hash =[ordered] @{PuertoRico = "San Juan" ; Army = "The Best" ; Motivation = " Success" }
You can also cast an ordered dictionary to a hashtable, but you cannot recover the ordered attribute, even if you clear the variable and enter new values. To re-establish the order, you must remove and recreate the variable.
example:
[hashtable] $hash =[ordered] @{PuertoRico = "San Juan" ; Army = "The Best" ; Motivation = " Success" }
To Display a list of methods that you can use with a hast table type the name of the variable and then pipe it to the Get-Member CmdLet,
Example:
$hash | Get-Member
Once you type that, you will see that you have access to have a whole bunch of properties to include:
Add, Clear, Clone, Contains, Etc.. Another hack is that when you use the dot notation to check for the method you can use the tab key to display them on Powershell.
To Display the hastable just type the name of the hash table example:
$hash
You need to use the .notation to display the key properties for example
$hash.keys
If you want to display the values type:
$hash.Values
You can also display the value of a specific key by using the key name as a property for example.
$hash.Motivation
If you need to count the number of key and value pair in the hash table type the count property. Example:
$hash.count
Hash table tables are not arrays, so you cannot use an integer as an index into the hash table, but you can use a key name to index into the hash table.
If the key is a string value, enclose the key name in quotation marks. Example:
$hash["Army"]
To use a loop in a hash table use the example below:
foreach ($Key in $hash.Keys) {
"The value of '$Key' is: $($hash[$Key])"
}
or
$hash.Keys | ForEach-Object {
"The value of '$_' is: $($hash[$_])"
}
If you want to add more stuff to the hash the format would be.
$hash["Addition_1"] = "ADD1"
or you can use the actual Method to add
Add(Key,Value) for example:
$hash.Add("Addition_2", "ADD2")
You can also add them by using the + operator. Example below:
$hash = $hash + @{Addition_3 = "Add3"}
You can also store stuff in variables and use the Add Method to add them. Example:
$t = "Today"
$now = (Get-Date)
$hash.Add($t, $now)
To Remove a hash Key you need to use the remove method for example:
$hash.Remove($t)
or
$hash.Remove("Army")
Browsing around the internet researching I also found that there is a different way to create Dictionaries in Powershell but it is not common to use:
This example came from TutorialsPoint.com
$countrydata = New-Object System.Collections.Generic.Dictionary"[String,Int]"
To Access the members again pipe the name of the variable to the Get-Member Cmdlet to see if you see any differences.
Looking around the internet you can also change the Value of a certain value by using the Set_Item() method. Example Below:
$hashtable.Set_Item("Key1", "Value for Key 1 Changed")
For Tasks:
Task1: Create 5 different HashTables ( 2 empty ones and 3 with 3 different keys and values inside of them)
Task2: Use the add Method to insert 2 new keys to all 5 hash tables.
Task3: Use the Remove Method and remove 1 Key from each of the hash tables.
Task4: Use a Foreach Loop to iterate through each of the hash tables:
Task5: Research more into Hashtables and come up with 2 Scenarios that you think hash tables would be a great use and why would be a great use?
Task6: Add all links that you have to a document and keep the links in Order.
This is all for now, Hopefully you learn something about Hash Tables:
Sources:
https://www.tutorialspoint.com/how-to-create-a-dictionary-in-powershell
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-5.0
https://www.educba.com/powershell-dictionary/
https://www.script-example.com/en-powershell-hashtable
PEACE!!