2017-07-06 84 views
0

我知道我在HKEY_LOCAL_MACHINE/SOFTWARE/CLASSES下有一个具有特定值的注册表项。但是,我不知道密钥。 (这是我试图查找的GUID。)以下代码可用于获取此值,但速度很慢,看起来不够优雅。有没有更好的方法来做到这一点?在PowerShell中查找具有特定名称值的注册表项

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

回答

3

该递归是什么杀死你。为了减轻递归搜索类的负担,可以手动指定Depth。如果您知道您的密钥进入注册表层次结构有多少步骤,则可以显着降低速度。例如:

$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() 

您的最佳结果将与深度为1,但深度为2或3仍然会比没有指定更快。

+0

我在2的深度;所以这比以前更好。但是,有没有办法在找到第一个时停止搜索?这可能会改善事情甚至更好。 – afeygin

+0

@afeygin可能有办法做到这一点,但我不知道它。如果我找到方法,我会让你。此外,深度参数仅适用于PowerShell v5之后。如果您需要替代方案,您可以执行以下操作:Get-ChildItem HKLM:\ Software \ Classes \\ * \\ * \\ * –

+0

我发现可以通过查看“HKLM:\ Software \ Classes \ Installer \ Features“而不是”HKLM:\ Software \ Classes“。 – afeygin

相关问题