如何在PowerShell中卸载多个registryconfiguration单元? (访问被拒绝错误)

我已经加载了几十个registryconfigurationHKLM:\temp_$username\ 。 现在当我尝试运行ls HKLM:\temp_* | %{reg unload $_} ls HKLM:\temp_* | %{reg unload $_}我得到ERROR: Access is denied.

我运行提升priveleges的PowerShell,并尝试重新启动PowerShell ISE清除variables。 我仍然收到错误。 我可以使用GUI卸载蜂箱,我可以运行单个蜂箱的命令( reg unload HKLM\temp_jimbob ),所以我有点困惑,为什么命令不会运行多个蜂箱。

什么导致访问被拒绝的错误,我该如何解决?

命令ls HKLM:\temp_*是什么导致“访问被拒绝”错误。 运行此命令将使每个所需的registryconfiguration单元都处于打开状态并且无法访问registry程序。 这是一个catch-22:没有列表蜂箱不能被卸载,但列表不能被获得。

要解决这个问题,请使用Name属性,它是一个[System.String][Microsoft.Win32.RegistryKey]

 $foo = ls "hklm:\temp_*" | Select -ExpandProperty Name foreach($bar in $foo) { reg unload $bar } 

请注意,它必须是两行。 尝试pipe道select的输出将导致相同的错误。

如果仍然收到错误,可能值得使用[gc]::collect()运行垃圾收集,通过Get-Variables ,或者重新启动PowerShell ISE。