If you’ve ever built a PowerShell hash table to store configuration data, user information, or server details, you’ve probably run into this scenario: you need to find something, but you’re not sure which key it’s under—or maybe you need to search through all the values to see what matches. Hash tables are fantastic for quick lookups when you know the key, but searching through them? That’s where things get interesting.
In this tutorial, I’ll show you several practical methods for finding strings in PowerShell hash tables—whether you’re searching keys, values, or both. I will also explain some best practices that you should follow.
What’s a Hash Table in PowerShell?
A hash table in PowerShell (also called a dictionary or associative array) stores data as key-value pairs. Think of it like a mini database where each key is unique and points to a value.
Here’s a simple example:
$users = @{
"BrandonGandy" = "Developer"
"SarahMiller" = "Manager"
"JohnDoe" = "Developer"
"EmilyRoads" = "Designer"
}Check out Convert String to Hashtable in PowerShell
Now, let’s explore how to search through this data using several methods.
Method 1: Check if a Specific Key Exists
This is the simplest scenario: you want to know if a particular key is in your hash table. You can use the .ContainsKey() method:
if ($users.ContainsKey("BrandonGandy")) {
Write-Host "Found Brandon Gandy!"
}Here is the exact output in the screenshot below.

Or use the -contains operator on .Keys:
if ($users.Keys -contains "BrandonGandy") {
Write-Host "Found Brandon Gandy!"
}Pro Tip
.ContainsKey() is faster and more explicit—I recommend it for clarity and performance, especially with large hash tables.
Read PowerShell Hashtable vs Array
Method 2: Search for a Value in the Hash Table
What if you want to find all keys where the value equals “Developer”? Here’s how.
You can use the .GetEnumerator() to loop through key-value pairs and filter in a hash table:
$users.GetEnumerator() | Where-Object { $_.Value -eq "Developer" }Output:
Name Value
---- -----
BrandonGandy Developer
JohnDoe DeveloperYou can also just get the keys by using the script below:
$developerKeys = $users.GetEnumerator() | Where-Object { $_.Value -eq "Developer" } | Select-Object -ExpandProperty NameDon’t try $users.Values -eq "Developer" and expect to get keys back—it only returns matching values. You need .GetEnumerator() to access both keys and values together.
Check out PowerShell Array of Hashtables
Method 3: Search Keys or Values with Wildcards (Partial Matches)
Real-world data isn’t always clean. Maybe you want to find all keys from the PowerShell hashtable that contain “Road” or values that start with “Dev”.
Search keys containing a substring:
$users.Keys | Where-Object { $_ -like "*Road*" }Output:
EmilyRoadsYou can also see the exact output in the screenshot below:

Search values with wildcard matching:
$users.GetEnumerator() | Where-Object { $_.Value -like "Dev*" }Output:
Name Value
---- -----
BrandonGandy Developer
JohnDoe DeveloperPro Tip: Use Regex for Advanced Searches
If you need case-sensitive or complex pattern matching, use -match:
$users.Keys | Where-Object { $_ -match "^Emily" }This finds keys that start with “Emily”.
Check out Format an Array as an HTML Table in PowerShell
Method 4: Filter Hash Tables and Create a New One
Sometimes you want to extract matching entries into a new hash table for further processing in PowerShell.
You can use the PowerShell script below.
$developers = @{}
$users.GetEnumerator() | Where-Object { $_.Value -eq "Developer" } | ForEach-Object {
$developers[$_.Name] = $_.Value
}Now $developers contains only the Developer entries.
Alternative: One-Liner with a Cast
$developers = @{
($users.GetEnumerator() | Where-Object { $_.Value -eq "Developer" }).ForEach({ $_.Name, $_.Value })
}This is more advanced, but handy when you get comfortable with PowerShell’s pipeline.
Pro Tips & Best Practices
- Use .GetEnumerator() for Filtering: It’s the cleanest way to access both keys and values. Without it, you’re stuck with just keys or just values.
- Watch Out for Case Sensitivity: By default,
-eqand-likeare case-insensitive in PowerShell. If you need case-sensitive matching, use-ceqor-cmatch. - Don’t Modify Hash Tables While Looping: If you try to add or remove keys during a
foreachloop over the hash table, you’ll get an error. Instead, collect your matches first, then modify:
$toRemove = $users.GetEnumerator() | Where-Object { $_.Value -eq "Developer" } | Select-Object -ExpandProperty Name
$toRemove | ForEach-Object { $users.Remove($_) }- Performance Matters with Large Hash Tables: If you’re searching repeatedly, consider restructuring your data. For example, if you often search by value, create a reverse lookup hash table where values are keys.
- Null and Empty Values: Always check if your hash table or values might be
$null. Use:
if ($users -and $users.ContainsKey("SomeKey")) { ... }Conclusion
In this tutorial, I explained how to find strings in PowerShell Hash Tables using various methods with examples. In my experience, you can use the .GetEnumerator() and Where-Object to work with hash tables in PowerShell.
Your turn: Try these methods with your own hash tables and see what you can find. Got a tricky scenario or a question? Drop a comment—I’d love to help!
You may also like the following tutorials:
- Find Passwords in Files with PowerShell
- Find Unquoted Service Paths with PowerShell
- PowerShell Find Location of Executable
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.