以下函数实际上可以解决问题:
function Remove-AllItemProperties([String] $path) {
Get-ItemProperty $path | Get-Member -MemberType Properties | Foreach-Object {
if (("PSChildName","PSDrive","PSParentPath","PSPath","PSProvider") -notcontains $_.Name) {
Remove-itemproperty -path $path -Name $_.Name
}
}
}
例如:要从注册表中删除所有键入的 url,您可以使用
Remove-AllItemProperties("HKCU:\SOFTWARE\Microsoft\Internet Explorer\TypedURLs")
我的问题是:
谢谢你的想法!
最佳答案
I wonder if there is not a more beautiful (i.e. compact solution for the problem.
我会简单地使用
Remove-ItemProperty -Name *
:function Remove-AllItemProperties
{
[CmdletBinding()]
param([string]$Path)
Remove-ItemProperty -Name * @PSBoundParameters
}
Remove-ItemProperty -Name *
将删除注册表项中的任何现有值 $Path
.[CmdletBinding()]
属性会自动将通用参数( -Verbose
、 -Debug
、 -ErrorAction
等)添加到您的函数中。通过喷溅
$PSBoundParameters
到内部调用,您会自动将这些选项直接传递给 Remove-ItemProperty
The functions throws an error if the item (registry key) has no properties (Get-Member complains about a missing object).
上面的方法不会有这个问题
关于Powershell - 删除注册表项中的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557634/