C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PowerShell Hast tableThe 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. SyntaxThe following statement is the syntax to create the Hashtable: $variable_name = @{ <key1> = <value1> ; < key2> = <value2> ; ..... ; < keyN> = <valueN>;} The following statement is the syntax to create an ordered dictionary: $variable_name = [ordered] @{ < key1> = <value1> ; < key2> = <value2> ; ..... ; < keyN> = <valueN>;} Create a Hash TableThe 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: $variablename = @{} 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. $student = @{ name = "Abhay" ; Course = "BCA" ; Age= 19 } Display a Hash table
$Student Output: Name Value ---- ----- Course BCA name Sumit Age 20
$Student.keys Output: Course name Age The following example displays all the values of the above example: $Student.values Output: BCA Sumit 20
$Student.count Output: 3
Next TopicPowerShell Operators
|