Hashtables (Associative Arrays)

Hashtable Definitions

PowerShell hashtables (also called associative arrays) allow you to associate keys with values. To define a hashtable, use the syntax:

$myHashtable = @{}

You can initialize a hashtable with its key/value pairs when you create it. PowerShell assumes that the keys are strings, but the values may be any datatype.

$myHashtable = @{ Key1 = "Value1"; "Key 2" = 1,2,3 }

Hashtable Access

To access or modify a specific element in an associative array, you may use either the array-access or property-access syntax:

$myHashtable["Key1"]

Returns "Value1".

$myHashtable."Key 2"

Returns the array 1,2,3.

$myHashtable["New Item"] = 5

Adds "New Item" to the hashtable.

Get Windows PowerShell Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.