PowerShell Hash Tables: Syntax and Examples
A hash table stores values by name, like a small lookup table. You'll see them everywhere in PowerShell, from settings to parameters.
Create and read
Use @{ } with key = value pairs. Read a value with square brackets or dot notation.
$ages = @{ Ana = 31; Ben = 27 }
$ages['Ana']
$ages.Ben
Add, change, remove
Assigning to a new key adds it. Assigning to an existing key changes it. Remove takes the key out.
$ages['Cy'] = 40
$ages['Ben'] = 28
$ages.Remove('Ana')
Loop over it
GetEnumerator gives you each key and value pair.
$ages.GetEnumerator() | ForEach-Object { "$($_.Key) is $($_.Value)" }
Practice it for real. shellreps has hands-on PowerShell labs that run in your browser, no install. The first one is free, no signup. The terminal mimics PowerShell syntax so the habits carry over, though it isn't Microsoft's PowerShell itself.