Hash Tables in Windows PowerShell

Hash Tables in Windows PowerShell

A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs. For example, a hash table can contain a series of Computer names and IP addresses where the Computer Names are the keys and the IP Addresses are the values, or vice versa.

In Windows PowerShell, each hash table is a System.Collections.Hashtable object.  You can use the properties and methods of Hashtable objects in Windows 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. You can also create nested hash tables, in which the value of a key is another hash table.

Ordered dictionaries differ from hash tables in that the keys always appear in the order in which you list them. The order of keys in a hash table is not determined.

Hash tables are frequently used because they are very efficient for finding and retrieving data. You can use hash tables to store lists and to create calculated properties in Windows PowerShell. Windows PowerShell gives a ConvertFrom-StringData cmdlet, which converts strings to a hash table.

The syntax of a hash table is as follows:

@{ <name> = <value>; <name> = <value> …}

Creating Hash Tables

To create a hash table, follow these guidelines:

  1. Begin the hash table with an at sign (@).
  2. Enclose the hash table in curly braces ({}).
  3. Enter one or more key/value pairs for the content of the hash table.
  4. Use an equal sign (=) to separate each key from its value.
  5. Use a semicolon (;) or a line break to separate the key/value pairs.
  6. The Keys that contain spaces must be enclosed in quotation marks. The Values must be valid Windows PowerShell expressions. The Strings must appear in quotation marks, even if they do not include spaces.
  7. To manage the hash table, save it in a variable.
  8. When assigning an ordered hash table to a variable, place the [ordered] attribute before the “@” symbol. If you place it before the variable name, the command fails.

For example, to create an empty hash table in the value of $hash, use $hash = @{} command.

PS D:\MyScripts> $hash = @{}

You can also add keys and values to a hash table when you create it. For example, the following statement creates a hash table with two keys.

PS D:\MyScripts> $hash = @{"ComputerName"="DC1";"IP Address"="192.168.10.10"}

Displaying Hash Table

To display a hash table that is saved in a variable, type the variable name. By default, a hash tables is displayed as a table with one column for keys and one for values.

PS D:\MyScripts> $hash

Name                           Value
----                           -----
ComputerName                   DC1
IP Address                     192.168.10.10

To get the members of a hash table, pipe the hash table to Get-Member cmdlet.

PS D:\MyScripts> $hash | Get-Member -MemberType Properties

   TypeName: System.Collections.Hashtable

Name           MemberType Definition
----           ---------- ----------
Count          Property   int Count {get;}
IsFixedSize    Property   bool IsFixedSize {get;}
IsReadOnly     Property   bool IsReadOnly {get;}
IsSynchronized Property   bool IsSynchronized {get;}
Keys           Property   System.Collections.ICollection Keys {get;}
SyncRoot       Property   System.Object SyncRoot {get;}
Values         Property   System.Collections.ICollection Values {get;}

Hash tables have Count, Keys and Values properties. You can use dot notation to display all count of key/value pairs or to display all of the keys or all of the values.

PS D:\MyScripts> $hash.count
2
PS D:\MyScripts> $hash.Keys
ComputerName
IP Address
PS D:\MyScripts> $hash.values
DC1
192.168.10.10

Each key name is also a property of the hash table, and its value is the value of the key-name property. Use the following format to display the property values.

PS D:\MyScripts> $hash.ComputerName
DC1

PS D:\MyScripts> $hash."IP Address"
192.168.10.10

Add or Remove the Keys and Values

To add keys and values to a hash table, use the following command format.

$hash["<key>”] = “<value>”

For example, to add a “DomainName” key with a value of “TechTutsOnline.com” to the hash table, use the following command:

PS D:\MyScripts> $hash.add("DomainName" , "TechTutsOnline.com")
PS D:\MyScripts>
PS D:\MyScripts> $hash

Name                           Value
----                           -----
IP Address                     192.168.10.10
DomainName                     TechTutsOnline.com
ComputerName                   DC1

You can add keys and values to a hash table by using the addition operator (+). For example, the following statement adds an “OperatingSystem” key with a value of “Server 2012 R2” to the hash table in the $hash variable.

PS D:\MyScripts> $hash = $hash + @{OperatingSystem = "Server 2012 R2"}
PS D:\MyScripts>
PS D:\MyScripts> $hash

Name                           Value
----                           -----
IP Address                     192.168.10.10
ComputerName                   DC1
OperatingSystem                Server 2012 R2
DomainName                     TechTutsOnline.com

You cannot use a subtraction operator (-) to remove a key/value pair from a hash table, but you can use the Remove method of the Hashtable object. The Remove method takes the key as its value.

For example, to remove the DomainName=TechTutsOnline.com key/value pair from the hash table in the value of the $hash variable, use the following command:

PS D:\MyScripts> $hash.Remove("DomainName")
PS D:\MyScripts> $hash

Name                           Value
----                           -----
IP Address                     192.168.10.10
ComputerName                   DC1
OperatingSystem                Server 2012 R2

In the same way you can use all of the properties and methods of Hashtable objects in Windows PowerShell.

Back



<p>Microsoft Certified Professional | Cisco Certified Network Associate</p>

Leave a Reply