Enumerate & Search Registry Key values with PowerShell
In most cases, PowerShell makes easy work out of working with the registry and its various components. However, it isn’t as easy if you want to enumerate & search registry key values. I was faced with this situation recently, so here is the solution I came up with.
The Problem
To put this example in perspective, this is what I wanted to achieve…
Find all the
HKCU\Volatile Environment
registry key for all environmental variables that contain the word View.
Why? Because I was writing a PowerShell script for a VMware Horizon View environment and needed to return all of the Horizon View specific data on a VDI machine.
Solution
I won’t bore you with the specifics of why I couldn’t simply use Get-ChildItem
or Get-ItemProperty
on their own, but here is the solution:
$RegKey = (Get-ItemProperty 'HKCU:\Volatile Environment')
$RegKey.PSObject.Properties | ForEach-Object {
If($_.Name -like '*View*'){
Write-Host $_.Name ' = ' $_.Value
}
}
Note: This is solution is also available as a 9to5IT Gists.
How it works
The script essentially allows you to search registry key values. How it works, is storing the specified registry key in a PSCustomObject
.
Each of the registry values are stored as NoteProperties
of the PSCustomObject. For this reason, we can’t simply enumerate all objects stored within the $RegKey
variable, as there is only one single object.
So, we need to enumerate all of the object’s properties instead. We do this via $RegKey.PSObject.Properties | ForEach-Object {
.
The next step it so find only the registry values that contain ‘View’ in their name. To do this we use PowerShell’s like comparison operator against each of the registry values returned.
And that’s how you enumerate & search registry key values using PowerShell.
Any questions or comments, let me know below.
How would I get the path to a key that contains a match for 2 values. This path is dynamic so I would need to find out the path to them before I try to manupulate them.
$SearchProfileList | % {
foreach ($ProfileImagePath in ($.ProfileImagePath)){
if ($.ProfileImagePath -like “C:\Users“)
{
Write-Host “SIM – $ProfileImagePath”
}
else
{
Write-Host “NOT – $ProfileImagePath”
}
}
}
This exactly what I look for!
Excellent Thank you for the guide.
I could have saved a lot of time if I found this earlier.