PowerShell Hashtable vs Array vs Dictionary

 

If you want to store specific values for an array, you will reach the limits with arrays. A hashtable is similar to an array, but stores a value for each entry. (similar to the vbscript: Dictionary)

Create hashtable

In the following example I create a hastable with some entries and their values:

$hashtable = @{"Entry1" = "Valuefor1"; "Entry2" = "Valuefor2"; "Entry3" = "Valuefor3"}

Output:

PS C:\Users\LiBe> $hashtable = @{"Entry1" = "Valuefor1"; "Entry2" = "Valuefor2"; "Entry3" = "Valuefor3"}
PS C:\Users\LiBe> $hashtable

Name                           Value
----                           -----
Entry3                         Valuefor3
Entry2                         Valuefor2
Entry1                         Valuefor1

additional entries

can be added with the following command:

Output:

PS C:\Users\LiBe> $hashtable.Add("additional Entry", "Value for additional Entry")
PS C:\Users\LiBe> $hashtable

Name                           Value
----                           -----
Entry3                         Valuefor3
Entry2                         Valuefor2
additional Entry               Value for additional Entry
Entry1                         Valuefor1

Remove entries:

$hashtable.Remove("Entry2")

Output:

PS C:\Users\LiBe> $hashtable.Remove("Entry2")
PS C:\Users\LiBe> $hashtable

Name                           Value
----                           -----
Entry3                         Valuefor3
additional Entry               Value for additional Entry
Entry1                         Valuefor1

Change entries:

$hashtable.Set_Item("Entry3", "Value for Entry 3 changed")

Output:

PS C:\Users\LiBe> $hashtable.Set_Item("Entry3", "Value for Entry 3 changed")
PS C:\Users\LiBe> $hashtable

Name                           Value
----                           -----
Entry3                         Value for Entry 3 changed
additional Entry               Value for additional Entry
Entry1                         Valuefor1

Read entry

$hashtable.Get_Item("Entry3")

Output

PS C:\Users\LiBe> $hashtable.Get_Item("Entry3")
Value for Entry 3 changed
PS C:\Users\LiBe>

Test if an entry exists

$hashtable.ContainsKey("Entry3")

Output

PS C:\Users\LiBe> $hashtable.ContainsKey("Entry3")
True

Test whether a certain value is present

$hashtable.ContainsValue("Wert für Eintrag3 geändert")

 Output

PS C:\Users\LiBe> $hashtable.ContainsValue("Value for Entry 3 changed")
True
PS C:\Users\LiBe>

Hashtable as Object Array:Pipe

For further use as an object array, e.g. to process it via a pipe, the method GetEnumerator is required.

$hashtable.GetEnumerator() | Sort-Object Name

 Output

PS C:\Users\LiBe> $hashtable.GetEnumerator() | Sort-Object Name

Name                           Value
----                           -----
additional Entry               Value for additional Entry
Entry1                         Valuefor1
Entry3                         Value for Entry 3 changed
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments