0

I know I have a registry entry under HKEY_LOCAL_MACHINE/SOFTWARE/CLASSES that has a certain value. However, I don't know the Key. (This is a GUID that I am trying to look up.) The following code works to get to this value, but it's slow and seems inelegant. Is there a better way of doing it?

$key = Get-ChildItem -Path "HKLM:\SOFTWARE\Classes" -Recurse | Get-ItemProperty -Name "FooBar" -ErrorAction @{}
$codeGuid = $key.PsChildName

1 Answer 1

3

The recurse is what kills you. To lighten the burden of recursively searching Classes, you can manually specify a Depth. If you KNOW how many steps your key is into the Registry Hierarchy, you can cut the speed significantly. For example:

$Timer = New-Object System.Diagnostics.Stopwatch

######NORMAL#######
#Completes in 70.2157527 Seconds on my system
$Timer.Start()
    Get-ChildItem -Path "HKLM:\Software\Classes" -Recurse | Get-ItemProperty -Name "AlwaysShowExt" -ErrorAction SilentlyContinue | Out-Null
$Timer.Stop()
$Timer.Elapsed
$Timer.Reset()

######DEPTH 1######
#Completes in 12.7461096 Seconds on my system
$Timer.Start()
    Get-ChildItem -Path "HKLM:\Software\Classes" -Recurse -Depth 1 | Get-ItemProperty -Name "AlwaysShowExt" -ErrorAction SilentlyContinue | Out-Null
$Timer.Stop()
$Timer.Elapsed
$Timer.Reset()

######DEPTH 2######
#Completes in 33.9162044 Seconds on my system
$Timer.Start()
    Get-ChildItem -Path "HKLM:\Software\Classes" -Recurse -Depth 2 | Get-ItemProperty -Name "AlwaysShowExt" -ErrorAction SilentlyContinue | Out-Null
$Timer.Stop()
$Timer.Elapsed
$Timer.Reset()

Your best results are going to be with a Depth of 1, but Depths of 2 or 3 will still be faster than not specifying.

Sign up to request clarification or add additional context in comments.

3 Comments

I am at a depth of 2; so this is better than before. But is there a way to stop the search once the first is found? That may improve things even better.
@afeygin There's probably a way to do it but I'm not aware of it. I'll let you if I find a way. Also, the Depth param only works after Powershell v5. If you need an alternative you can do something like: Get-ChildItem HKLM:\Software\Classes\*\*\*
I found that I could locate the key a lot faster by looking in "HKLM:\Software\Classes\Installer\Features" instead of just "HKLM:\Software\Classes".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.