Hash Tables

Hash Tables in PowerShell: Hash tables store highly structured Key-Value pairs. They are created using .

Hash tables store highly structured Key-Value pairs. They are created using @{}.

# Defining a Hash Table
$userConfig = @{
    Username = "Admin";
    Retries  = 5;
    Enabled  = $true
}

# Accessing fields via dot notation
Write-Output $userConfig.Username    # Admin
Write-Output $userConfig.Retries     # 5

# Adding/Modifying
$userConfig.Domain = "CORP"

# Looping through a Hash Table via GetEnumerator()
foreach ($entry in $userConfig.GetEnumerator()) {
    Write-Output "Key: $($entry.Key) | Value: $($entry.Value)"
}