PowerShell Hast table
The PowerShell Hashtable is a data structure that stores one or more key/value pairs. It is also known as the dictionary or an associative array.
In the PowerShell, there exists a Hashtable (System.Collections.Hashtable) object for each hash table.
We can use the properties and methods of the Hashtable object in the PowerShell. The key and the values in the Hash table are also the objects of .NET type.
After the PowerShell version 3.0 introduced, we can use the [ordered] attribute to create an ordered dictionary (System.Collections.Specialized.OrderedDictionary) in PowerShell.
The main difference between the ordered dictionaries and the Hashtable is that the keys in dictionaries always appear in the order in which we list them. But the order of the keys in the Hashtable is not determined.
The following statement is the syntax to create the Hashtable:
The following statement is the syntax to create an ordered dictionary:
Create a Hash Table
The following are the steps to create the hash table in the PowerShell:
If you want to create an empty hash table, type the following command in the PowerShell:
We can also add the keys and values to the hash table when we create it.
The following example describe how to create the hash table with three keys and their values.
Display a Hash table
Name Value ---- ----- Course BCA name Sumit Age 20
Course name Age
The following example displays all the values of the above example:
BCA Sumit 20