PowerShell Hash Tables
A hash table stores data as key-value pairs. Each item has a unique key and an associated value. Think of a hash table like a dictionary — look up a word (key) and get its definition (value). Hash tables are one of the most versatile data structures in PowerShell for storing, retrieving, and organizing structured information.
Hash Table Structure
@{
Key1 = Value1
Key2 = Value2
Key3 = Value3
}
Example:
@{
Name = "Rahul"
Age = 30
City = "Pune"
IsActive = $true
}
Creating a Hash Table
# Basic hash table
$person = @{
Name = "Rahul"
Age = 30
City = "Pune"
}
# Display the hash table
$person
Output:
Name Value
---- -----
Name Rahul
Age 30
City Pune
Accessing Values
$server = @{
Hostname = "WebServer01"
IP = "192.168.1.10"
Status = "Online"
Port = 80
}
# Method 1 – Dot notation
Write-Host $server.Hostname # WebServer01
Write-Host $server.Status # Online
# Method 2 – Square bracket notation
Write-Host $server["IP"] # 192.168.1.10
Write-Host $server["Port"] # 80
Adding and Updating Key-Value Pairs
$config = @{
Theme = "Dark"
Language = "English"
}
# Add a new key
$config["Version"] = "3.0"
$config.Region = "India"
# Update an existing key
$config["Theme"] = "Light"
Write-Host $config["Theme"] # Light
Write-Host $config["Version"] # 3.0
Write-Host $config.Region # India
Removing Keys
$profile = @{
Username = "admin"
Password = "secret123"
LastLogin = "2026-03-20"
}
# Remove a key
$profile.Remove("Password")
# Verify it is gone
$profile.ContainsKey("Password") # False
Checking Keys and Values
$settings = @{
AutoSave = $true
BackupDir = "C:\Backup"
MaxLogs = 100
}
# Check if a key exists
$settings.ContainsKey("AutoSave") # True
$settings.ContainsKey("Theme") # False
# Check if a value exists
$settings.ContainsValue(100) # True
$settings.ContainsValue("C:\Temp") # False
Hash Table Properties
$data = @{
A = 1
B = 2
C = 3
}
# Number of key-value pairs
Write-Host $data.Count # 3
# Get all keys
Write-Host $data.Keys # A B C
# Get all values
Write-Host $data.Values # 1 2 3
Iterating Over a Hash Table
Loop Through All Keys
$employee = @{
Name = "Sanya"
Department = "IT"
Salary = 65000
Location = "Bangalore"
}
foreach ($key in $employee.Keys) {
Write-Host "$key : $($employee[$key])"
}
Output:
Name : Sanya
Department : IT
Salary : 65000
Location : Bangalore
Using GetEnumerator
foreach ($entry in $employee.GetEnumerator()) {
Write-Host "Key=$($entry.Key) Value=$($entry.Value)"
}
Ordered Hash Table
Standard hash tables do not guarantee the order in which keys are stored. Use [ordered] to keep keys in the insertion order.
# Standard hash table – order not guaranteed
$standard = @{
First = 1
Second = 2
Third = 3
}
# Ordered hash table – keys stay in insertion order
$ordered = [ordered]@{
First = 1
Second = 2
Third = 3
}
$ordered.Keys # Output: First Second Third (always in this order)
Nested Hash Tables
Hash table values can themselves be hash tables, creating a nested structure for complex data.
$company = @{
Name = "TechCorp"
CEO = @{
Name = "Amir Khan"
Email = "amir@techcorp.com"
}
Office = @{
City = "Hyderabad"
Country = "India"
Zip = "500001"
}
}
# Access nested values
Write-Host $company.Name # TechCorp
Write-Host $company.CEO.Name # Amir Khan
Write-Host $company.Office.City # Hyderabad
Write-Host $company["Office"]["Country"] # India
Hash Table vs Array – When to Use Which
| Feature | Array | Hash Table |
|---|---|---|
| Stores | Indexed list of values | Named key-value pairs |
| Access by | Numeric index (0, 1, 2…) | Key name ("Name", "IP"…) |
| Order | Always ordered | Unordered (use [ordered] to fix) |
| Best for | Simple lists | Structured records / config data |
| Example | @("A","B","C") | @{Name="A"; Code=1} |
Hash Table as Parameter Splat
Splatting passes a hash table of parameters to a cmdlet. This makes long commands clean and readable.
# Without splatting – hard to read
New-Item -Path "C:\Reports" -Name "Summary.txt" -ItemType File -Force
# With splatting – clean and organized
$params = @{
Path = "C:\Reports"
Name = "Summary.txt"
ItemType = "File"
Force = $true
}
New-Item @params # Note: @ not $ when calling with splat
Convert Hash Table to PSCustomObject
Hash tables convert easily to PowerShell custom objects, which work better with cmdlets that expect objects.
$hashData = @{
ProductID = "P001"
ProductName = "Mechanical Keyboard"
Price = 2500
Stock = 50
}
# Convert to PSCustomObject
$product = [PSCustomObject]$hashData
# Access like an object
Write-Host $product.ProductName # Mechanical Keyboard
Write-Host $product.Price # 2500
# Export to CSV (works with objects, not raw hash tables)
$product | Export-Csv -Path "C:\products.csv" -NoTypeInformation
Array of Hash Tables
# Common pattern: list of records
$users = @(
@{ Name = "Alice"; Role = "Admin"; Active = $true },
@{ Name = "Bob"; Role = "User"; Active = $false },
@{ Name = "Carol"; Role = "Admin"; Active = $true }
)
# Display each user
foreach ($user in $users) {
Write-Host "$($user.Name) is a $($user.Role) - Active: $($user.Active)"
}
# Filter: active users only
$users | Where-Object { $_.Active -eq $true } | ForEach-Object {
Write-Host "Active: $($_.Name)"
}
Output:
Alice is a Admin - Active: True
Bob is a User - Active: False
Carol is a Admin - Active: True
Active: Alice
Active: Carol
Summary
Hash tables store data as named key-value pairs — ideal for configuration objects, server records, parameter splats, and nested data structures. Ordered hash tables preserve insertion order. Nested hash tables model complex hierarchical data. Converting hash tables to PSCustomObject unlocks full pipeline compatibility. Hash tables paired with arrays cover almost every structured data need in a PowerShell script.
