Skip to content

Hash Tables

A hash table is a collection of key–value pairs. Each key is a unique name, and each key maps to a value. Hash tables are used to store structured data, configuration settings, and named attributes that belong together. PowerShell treats hash tables as first‑class objects, and they are essential for building clean, maintainable scripts.


1. Hash table syntax

A hash table is created using the @{} syntax.

Inside the braces, each entry uses the format:

Key = Value

Each entry is separated by a newline or semicolon.

Example:

$person = @{
    Name = "Alice"
    Age  = 30
    City = "Osaka"
}

Explanation:

  • $person is the variable holding the hash table.
  • Name, Age, and City are keys.
  • "Alice", 30, and "Osaka" are the values associated with those keys.

PowerShell automatically creates a System.Collections.Hashtable object.

You can confirm this:

$person.GetType().Name

This prints:

Hashtable

2. Accessing values

There are two ways to retrieve a value from a hash table.

2.1 Bracket notation

$person["Name"]

This returns:

Alice

2.2 Dot notation

$person.Name

This also returns "Alice".

Dot notation is shorter, but bracket notation is required when:

  • The key contains spaces
  • The key contains special characters
  • The key is stored in a variable

Example:

$key = "City"
$person[$key]

This returns "Osaka".


3. Adding and modifying entries

3.1 Adding a new key

$person["Email"] = "alice@example.com"

This creates a new entry with the key Email.

3.2 Modifying an existing key

$person.Age = 31

This updates the value associated with the key Age.

3.3 Removing a key

$person.Remove("City")

This deletes the key City and its value.


4. Iterating through a hash table

A hash table can be enumerated to inspect all keys and values.

4.1 Enumerating key–value pairs

foreach ($entry in $person.GetEnumerator()) {
    "$($entry.Key): $($entry.Value)"
}

Explanation:

  • $person.GetEnumerator() returns each key–value pair.
  • $entry.Key is the key.
  • $entry.Value is the value.
  • The loop prints each pair on its own line.

4.2 Enumerating only keys

foreach ($key in $person.Keys) {
    $key
}

4.3 Enumerating only values

foreach ($value in $person.Values) {
    $value
}

5. Typical use cases

5.1 Storing structured information

Hash tables are ideal for grouping related settings:

$config = @{
    RetryCount = 3
    Timeout    = 10
    LogPath    = "C:\Logs"
}

Each key represents a setting, and the hash table keeps them organized.

5.2 Passing parameters using splatting

Hash tables can be expanded into parameters using @:

$params = @{
    Path   = "C:\Windows"
    Filter = "*.exe"
}

Get-ChildItem @params

Explanation:

  • $params contains named parameters.
  • @params “splats” the hash table into the command.
  • PowerShell treats each key as a parameter name and each value as its argument.

This makes scripts cleaner and easier to maintain.

5.3 Creating custom objects

Hash tables are often used to build objects with named properties:

$user = New-Object psobject -Property @{
    Name   = "Bob"
    Active = $true
}

Explanation:

  • The hash table defines the properties.
  • New-Object converts the hash table into a structured object.
  • $user.Name and $user.Active now behave like normal object properties.