Hash Table

Using a hash table, you can store information as a set of keys and values.

Hash Table

Hash table can store information as a set of keys and values.
Hash table does not guarantee the order of storage.

How to create an hash table

# Normal example
PS> $hash = @{
>>   Key1 = "Value1"
>>   Key2 = 1,2
>> }
PS> $hash
Name                           Value
----                           -----
Key2                           {1, 2}
Key1                           Value1

# Single line example. Separated by semicolons.
PS> $hash = @{Key1 = "Value1"; Key2 = 1,2}
PS> $hash
Name                           Value
----                           -----
Key2                           {1, 2}
Key1                           Value1

How to access the elements of hash table

# Property-based access methods  $hash.Key
PS> $hash.Key1
Value1
PS> $hash.Key2
1
2

# Accessing elements using index  $hash["Key"]
PS> $hash["Key1"]
Value1
PS> $hash["Key2"]
1
2

Adding elements to hash table

How to add keys and values.

# Property-based access methods  $hash.Key = Value
PS> $hash.Key3 = "NewValue3"
PS> $hash
Name                           Value
----                           -----
Key1                           Value1
Key2                           {1, 2}
Key3                           NewValue3
# Accessing elements using index   $hash["Key"] = Value
PS> $hash["Key4"] = "NewValue4"
PS> $hash                     
Name                           Value
----                           -----
Key3                           NewValue3
Key2                           {1, 2}
Key1                           Value1
Key4                           NewValue4

Removing elements of hash table

Use the Remove method to remove an element from hash table.

PS> $hash.Remove("Key3")
PS> $hash
Name                           Value
----                           -----
Key2                           {1, 2}
Key1                           Value1
Key4                           NewValue4

Clearing hash table

Using the Clear method to clear hash table.

PS> $hash.Clear()
PS> $hash
PS> 

Hash table with Guaranteed Order

If you want to guarantee the order in which the keys are stored, add [ordered].
Since the storage order is guaranteed, you can access by index value as in $table[number].
Available in PowerShell V3 and later.

PS> $ordered = [ordered]@{first=1;second=2}
PS> $ordered.first
1
PS> $ordered[0]
1

User-defined objects

To create a user-defined object, add [pscustomobject].
It can be sorted and selected like any other object.
Available in PowerShell V3 and later.

PS> $object = [PSCustomObject]@{Shell="PowerShell";Computer="Windows"}
PS> $object
 
Shell      Computer
-----      --------
PowerShell Windows

# Example of using a created user-defined object (CSV file output)
PS> $object | Export-CSV -Path ./object.csv

How to sort by key or value

By nature, hash tables are used to refer to values by keys.
If you still want to sort it, use GetEnumerator() method.

PS> $hash = @{key1 = 1; key2 = 2; key10 = 10; key3 = 3; key13 = 13}
PS> $hash
 
Name                           Value
----                           -----
key13                          13
key1                           1
key2                           2
key3                           3
key10                          10
 
PS> $hash.GetEnumerator() | sort value
 
Name                           Value
----                           -----
key1                           1
key2                           2
key3                           3
key10                          10
key13                          13

Hash table and foreach statement

To handle hash table in foreach statement, use $hash.Keys.

PS> $hash = @{key1 = 1; key2 = 2; key10 = 10; key3 = 3; key13 = 13}
PS> foreach ($key in $hash.Keys) {
>> "$($key):$($hash.$key)"
>> }
>>
key13:13
key1:1
key2:2
key3:3
key10:10

YouTube

Click here for a video explanation.

Everything you wanted to know about hashtables | Microsoft Docs

About Hash Tables | Microsoft Docs