我正在尝试从注册表项中删除所有值。在 PowerShell 中处理注册表时,我总是通过
LiteralPath
,除了 Remove-ItemProperty
之外,它在任何地方都工作正常。
这并不像我预期的那样工作:
Remove-ItemProperty -LiteralPath 'HKCU:Delete_all_values' -Name *
但常规的
-Path
有效:
Remove-ItemProperty -Path 'HKCU:Delete_all_values' -Name *
官方文档对此事保持沉默。对我做错了什么有什么建议吗?
您真正要寻找的是
Clear-Item
- 它将删除所有值,但保留键和任何子键完好无损:
Clear-Item -LiteralPath HKCU:\Delete_all_values
-Name *
不能与 -LiteralPath
一起使用?使用
-LiteralPath
会导致 *-ItemProperty
提供程序 cmdlet 抑制 所有通配符扩展 - 包括属性名称:
Get-ItemProperty -Path HKCU:\Delete_all_values -Name * # returns all properties
Get-ItemProperty -LiteralPath HKCU:\Delete_all_values -Name * # returns no properties (unless you have one with the literal name *)
如果您通过管道传输注册表项,您将观察到相同的行为,因为 PowerShell 默认情况下会将密钥的完整提供程序路径绑定到
-LiteralPath
:
Get-Item HKCU:\Delete_all_values |Get-ItemProperty -Name * # returns no properties (unless you have one with the literal name *)
将值名称显式传递给
-Name
将解决问题:
$targetKey = Get-Item -LiteralPath HKCU:\Delete_all_values
# piping a RegistryKey instance will also cause binding against -LiteralPath
$targetKey |Remove-ItemProperty -Name $targetKey.GetValuesNames()
此时您不妨使用
Clear-Item