Table of Contents
What is a Hash Table in 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.
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:
- Begin the hash table with an at sign @.
- Enclose the hash table in curly braces {}.
- Enter one or more key/value pairs for the content of the hash table.
- Use an equal sign = to separate each key from its value.
- Use a semicolon ; or a line break to separate the key/value pairs.
- 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.
- To manage the hash table, save it in a variable.
- 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 C:\> $hashtable = @{}
PS C:\> $hashtable
PS C:\>
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 C:\> $hashtable = @{"ComputerName"="DC1";"IP Address"="192.168.10.10"}
PS C:\> $hashtable
Name Value
---- -----
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 C:\> $hashtable
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 C:\> $hashtable | 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 C:\> $hashtable.count
2
PS C:\> $hashtable.keys
ComputerName
IP Address
PS C:\> $hashtable.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 C:\> $hashtable.ComputerName
DC1
PS C:\> $hashtable."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.
$hashtable["<key>"] = "<value>"
For example, to add a DomainName key with a value of bonguides.com to the hash table, use the following command:
PS C:\> $hashtable.add("DomainName" , "bonguides.com")
PS C:\> $hashtable
Name Value
---- -----
IP Address 192.168.10.10
DomainName bonguides.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 $hashtable variable.
PS C:\> $hashtable = $hashtable + @{OperatingSystem = "Server 2012 R2"}
PS C:\> $hashtable
Name Value
---- -----
IP Address 192.168.10.10
ComputerName DC1
OperatingSystem Server 2012 R2
DomainName bonguides.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=bonguides.com key/value pair from the hash table in the value of the $hashtable variable, use the following command:
PS C:\> $hashtable.Remove("DomainName")
PS C:\> $hashtable
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.
PS C:\> $hashtable | Get-Member
TypeName: System.Collections.Hashtable
Name MemberType Definition
---- ---------- ----------
Add Method void Add(System.Object key, System.Object value), void IDictionary.Add(Syste...
Clear Method void Clear(), void IDictionary.Clear()
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
Contains Method bool Contains(System.Object key), bool IDictionary.Contains(System.Object key)
ContainsKey Method bool ContainsKey(System.Object key)
ContainsValue Method bool ContainsValue(System.Object value)
CopyTo Method void CopyTo(array array, int arrayIndex), void ICollection.CopyTo(array arra...
Equals Method bool Equals(System.Object obj)
GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator(), System.Collections...
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Syst...
GetType Method type GetType()
OnDeserialization Method void OnDeserialization(System.Object sender), void IDeserializationCallback....
Remove Method void Remove(System.Object key), void IDictionary.Remove(System.Object key)
ToString Method string ToString()
Item ParameterizedProperty System.Object Item(System.Object key) {get;set;}
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;}