Powershell - 删除注册表项中的所有属性

Powershell - Remove all properties in a registry item

以下函数实际上可以解决问题:

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
        }
    }
}

例如:要从注册表中删除所有键入的网址,您可以使用

Remove-AllItemProperties("HKCU:\SOFTWARE\Microsoft\Internet Explorer\TypedURLs")

我的问题是:

  1. 由于我对 Powershell 比较陌生:我想知道是否有更漂亮的(即问题的紧凑解决方案。

  2. 如果项目(注册表项)没有属性(Get-Member 抱怨缺少对象),函数将抛出错误。

谢谢你的想法!

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).

上面的方法不会有这个问题