The hashtable tables are not to be considered normal arrays because you cannot use integer values for indexing; instead, you can use the name of a key. In PowerShell, the hashtable is denoted by the curly braces “{}”.
This article is presented to explain the basic concept of the PowerShell hashtable.
PowerShell Hashtable
In PowerShell, the hash table is used to store one or multiple lists. Hash Tables can be very helpful in finding and retrieving data. With the following hash table syntax, we will elaborate on the main purpose and use of hashtables.
Syntax
- Key: refers to the items/name
- Value: denotes the value passed to the items.
With help of the given-below examples, we will explain the functionalities and use of hashtables.
How to create a PowerShell hashtable?
The following example is used to create a simple hash table with a pair of keys and values.
"Apple" = 200
"Peach" = 250
"Mango" = 150
}
In the above script, we have declared a variable (i.e $Items) that contains the pair of keys and values, enclosed with curly braces. Among these, the keys are “Apple”, “Peach”, and “Mango” while 200, 250, and 150 come under the values of the hashtable.
The below code will execute the script file (hashtable.ps1) from the PowerShell terminal.
Once the PowerShell script file is executed then call the $Items to print the output in the console.
As you can see that the hashtable is created containing the pair of keys and values.
How to Add Keys and Values Pair to an Empty Hashtable?
In this example, we will see how to add the keys and values pair to an empty hashtable.
#$Items.Add($Key, $Value)
$Items.Add("Apple", 200)
$Items.Add("Mango", 150)
$Items.Add("Peach", 250)
$Items
In the above PowerShell script body, we have created an empty hashtable by assigning the $Items variable. With the “.Add()” method, some keys and values have been passed to the hashtable.
To execute the above script, copy the complete path to run the script file from the terminal.
As per the screenshot mentioned above, the pair of keys and values has been successfully added with the “.Add()” method of Powershell.
How to Access and Update the Existing KeysValues of the Hashtable?
One can access and update the existing keys and values of the hashtable. In the below script the values of the two $Items (i.e. Apple, Mango) will be updated with new values.
"Apple" = 200
"Peach" = 250
"Mango" = 150
}
$Items["Apple"] = 100
$Items["Mango"] = 100
Run the $Items from the terminal to print the results on the screen.
It can be observed from the output results that the values of Mango and Apple have been updated with the new values i.e. 100,100.
How to Remove Keys and Values from the Hashtable?
The specific keys and values of the hashtable can also be removed with the “.Remove()” method. Remember the Remove() method helps to remove the specific elements of an array.
"Apple" = 100
"Peach" = 250
"Mango" = 100
}
$Items.Remove("Peach")
$Items
The above-mentioned output states that the Peace has been removed from the hashtable while the rest of the elements are displayed in the console.
How to Count the Elements of the Hashtable?
PowerShell hashtable supports a bunch of methods for various tasks. Here, we are going to count the total number of elements that exist in the hashtable $Items. Simply, call the $Items.Count from the console and examine the output.
"Apple" = 100
"Peach" = 250
"Mango" = 100
}
$Items.Count
According to the results, we have only three (3) elements in the $Items.
How to Sum the Total Elements of the Hashtable?
One can find out the total sum of the element of the hashtable. Suppose we have three elements in the hashtable. Now, we want to calculate the total values of the hashtable (i.e. $Items). The below script will give an insight into the working of Sum.
"Apple" = 200
"Peach" = 250
"Mango" = 150
}
$Items.values | Measure-Object -Sum
The above output shows that the total sum of all three (3) elements is 600.
How to Get Average of the Hashtable Elements?
The given-below example will explain how to get the Average of the total keys and values. If we look at the below script, We have applied the values property on $Items and piped it with the -Average parameter of the Measure-Object parameter. Let’s get the results in the console.
"Apple" = 200
"Peach" = 250
"Mango" = 150
}
$Items.values | Measure-Object -Average
It can be examined in the above results, that the Average of the existing three elements is 200.
How to access the Keys and Values of Hashtable?
You can also iterate the hashtable to access the keys and values using the “.GetEnumeratror()” method. The following example script is given for reference.
"Apple" = 100
"Peach" = 250
"Mango" = 100
}
$Items.GetEnumerator() | ForEach-Object{
$Description = '{0} is {1} per KG' -f $_.key, $_.value
Write-Output $Description
}
In the following command, we have copied the absolute path of the script to execute it from the terminal.
The output, mentioned above, shows that the script is successfully executed and iterated each time to print all the elements of the hashtable.
Bonus Tip
If you want to explore more about the functionality of the $Items hashtable, pipe the $Items with the Get-Member command.
Here you go! You have learned to use the PowerShell hashtable and its various functionalities.
Conclusion
PowerShell hashtable provides a data structure in the form of key and value pairs. In this article, we have illustrated the core concept of the hashtable with practical examples. Moreover, we have explored some of the functionalities of the hashtable methods such as Sum, Average, Count, etc. This article also explains how to iterate a hashtable to access the keys and values.