该递归是什么杀死你。为了减轻递归搜索类的负担,可以手动指定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仍然会比没有指定更快。
我在2的深度;所以这比以前更好。但是,有没有办法在找到第一个时停止搜索?这可能会改善事情甚至更好。 – afeygin
@afeygin可能有办法做到这一点,但我不知道它。如果我找到方法,我会让你。此外,深度参数仅适用于PowerShell v5之后。如果您需要替代方案,您可以执行以下操作:Get-ChildItem HKLM:\ Software \ Classes \\ * \\ * \\ * –
我发现可以通过查看“HKLM:\ Software \ Classes \ Installer \ Features“而不是”HKLM:\ Software \ Classes“。 – afeygin