How to Convert Arrays to Hashtables in PowerShell?

If you’ve been working with PowerShell for a while, you’ve probably found yourself juggling arrays of data, such as lists of server names, user IDs, configuration values, etc. Arrays are great for storing ordered collections, but here’s the thing: when you need to look up a specific value or associate keys with values (like mapping usernames to email addresses), arrays can be slow and awkward. That’s where hashtables will be helpful!

In this tutorial, I’ll walk you through several practical methods for converting arrays into hashtables in PowerShell.

Below are the things you will learn here:

  • Why hashtables are often better than arrays for certain tasks
  • Method 1: Converting a simple array into a hashtable with custom keys
  • Method 2: Converting an array of objects into a hashtable
  • Method 3: Creating hashtables from two parallel arrays (keys and values)
  • Method 4: Using Group-Object to create grouped hashtables

Let’s dive in!

Arrays & Hashtables in PowerShell

Let us first understand what arrays and hash tables are in PowerShell.

Arrays are great for ordered lists in PowerShell. But when you want to retrieve an item by a specific identifier (rather than by its position), you have to loop through the array or use methods like Where-Object, which can be slow with large datasets.

Hashtables, on the other hand, store data as key-value pairs. Looking up a value by its key is nearly instantaneous, even with thousands of entries. They’re perfect for:

  • Mapping one value to another (e.g., usernames to departments)
  • Quick lookups without looping
  • Building configuration objects or structured data

Now, let’s explore how to convert your arrays into hashtables.

Method 1: Convert a Simple Array to a Hashtable with Custom Keys

Let’s say you have a simple array of server names using PowerShell:

$servers = @("Server01", "Server02", "Server03")

You want to convert this into a hashtable where each server name is a key, and maybe assign a default value (like status or priority).

Step-by-Step

Here is the complete PowerShell script:

# Create an empty hashtable
$serverHash = @{}

# Loop through the array and add each item
foreach ($server in $servers) {
    $serverHash[$server] = "Online"  # Assign a default value
}

# View the result
$serverHash

Output:

Name                           Value
----                           -----
Server01                       Online
Server02                       Online
Server03                       Online

You can even see the exact output in the screenshot below:

Convert Arrays to Hashtables in PowerShell

Now you can quickly check or update a server’s status:

$serverHash["Server02"]  # Returns "Online"
$serverHash["Server02"] = "Offline"  # Update the value

Pro Tip

If you just want the array items as keys with $null or $true as values (useful for quick membership tests), you can simplify:

$serverHash = @{}
$servers | ForEach-Object { $serverHash[$_] = $true }

This is great for checking “does this item exist?” without looping.

Method 2: Convert an Array of Objects to a Hashtable

This is where things get really useful. Let’s say you have an array of custom objects (maybe from Import-Csv or Get-ADUser):

$users = @(
    [PSCustomObject]@{ Username = "jdoe"; Email = "jdoe@example.com"; Department = "IT" },
    [PSCustomObject]@{ Username = "asmith"; Email = "asmith@example.com"; Department = "HR" },
    [PSCustomObject]@{ Username = "bjones"; Email = "bjones@example.com"; Department = "Sales" }
)

You want to create a hashtable where the Username is the key, and the entire object (or a specific property) is the value.

Step-by-Step

# Create an empty hashtable
$userHash = @{}

# Loop through and use Username as the key
foreach ($user in $users) {
    $userHash[$user.Username] = $user
}

# Now you can quickly look up a user
$userHash["jdoe"]

Output:

Username Email               Department
-------- -----               ----------
jdoe     jdoe@example.com    IT

Want just the email? Modify the assignment:

$userHash[$user.Username] = $user.Email

Pro Tip: One-Liner with ForEach-Object

You can do this more concisely using the pipeline:

$userHash = @{}
$users | ForEach-Object { $userHash[$_.Username] = $_ }

In my experience, this approach is super handy when importing CSV files, and you need instant lookups by ID or name.

Method 3: Convert Two Parallel Arrays (Keys and Values)

Sometimes you have two separate arrays — one for keys and one for values — and you want to zip them together into a hashtable.

$keys = @("Name", "Age", "City")
$values = @("Alice", 30, "Seattle")

Step-by-Step

$hash = @{}

for ($i = 0; $i -lt $keys.Count; $i++) {
    $hash[$keys[$i]] = $values[$i]
}

$hash

Output:

Name                           Value
----                           -----
Name                           Alice
Age                            30
City                           Seattle

Pro Tip: Make Sure Arrays Are the Same Length!

If your arrays have different lengths, you’ll either miss keys or get $null values. Always validate:

if ($keys.Count -ne $values.Count) {
    Write-Warning "Arrays must be the same length!"
}

Read Get the First and Last Line of a CSV File in PowerShell

Method 4: Using Group-Object to Create a Grouped Hashtable

Let’s say you have an array of objects and you want to group them by a property (like Department) into a hashtable.

$users = @(
    [PSCustomObject]@{ Username = "jdoe"; Department = "IT" },
    [PSCustomObject]@{ Username = "asmith"; Department = "HR" },
    [PSCustomObject]@{ Username = "bjones"; Department = "IT" }
)

Use Group-Object with the -AsHashTable parameter:

$groupedHash = $users | Group-Object -Property Department -AsHashTable

# Access all IT users
$groupedHash["IT"]

Output:

Username Department
-------- ----------
jdoe     IT
bjones   IT

Pro Tip: Add -AsString for String Keys

By default, Group-Object creates keys as objects. Add -AsString to ensure they’re strings, which can avoid subtle bugs:

$groupedHash = $users | Group-Object -Property Department -AsHashTable -AsString

This method is a lifesaver when you’re aggregating or categorizing data!

Check out PowerShell Cannot Index into a Null Array

Issues & Best Practices

Now, let me share the issues I faced while working on this and a few best practices you should follow.

Duplicate Keys Will Overwrite

Hashtables don’t allow duplicate keys. If your array has duplicates, the last value wins:

$hash = @{}
@("Server01", "Server01") | ForEach-Object { $hash[$_] = $true }
# $hash will only have one entry for "Server01"

Solution: Check for duplicates first, or use an array as the value:

if ($hash.ContainsKey($key)) {
    # Handle duplicate
}

Case Sensitivity

By default, PowerShell hashtables are case-insensitive. But if you create one manually with [hashtable]::new(), it might be case-sensitive depending on the comparer. Stick with @{} for typical use.

Use .ContainsKey() for Safe Checks

Before accessing a key, check if it exists:

if ($hash.ContainsKey("Server01")) {
    $hash["Server01"]
}

This prevents errors when a key might be missing.

Consider Ordered Hashtables for Readability

If you want your hashtable to maintain insertion order (useful for configs or output), use [ordered]:

$orderedHash = [ordered]@{}
$orderedHash["First"] = 1
$orderedHash["Second"] = 2

Conclusion

Converting arrays to hashtables in PowerShell is a small change that can make a huge difference in your scripts — especially when you’re dealing with lookups, mappings, or grouping data. We covered four main methods:

  1. Simple array to hashtable with custom values
  2. Array of objects to hashtable using a property as the key
  3. Two parallel arrays zipped into key-value pairs
  4. Grouped hashtables using Group-Object

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.