Search code examples
powershellregistry

Powershell - Remove all properties in a registry item


The following functions actually does the trick:

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

For example: To delete all typed urls from the registry you can use

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

My Problems are:

  1. Since im relatively new to Powershell: I wonder if there is not a more beautiful (i.e. compact solution for the problem.

  2. The functions throws an error if the item (registry key) has no properties (Get-Member complains about a missing object).

Thanks for your ideas!


Solution

  • I wonder if there is not a more beautiful (i.e. compact solution for the problem.

    I would simply use Remove-ItemProperty -Name *:

    function Remove-AllItemProperties
    {
        [CmdletBinding()]
        param([string]$Path)
    
        Remove-ItemProperty -Name * @PSBoundParameters
    }
    

    Remove-ItemProperty -Name * will remove any existing value in the registry key at $Path.

    The [CmdletBinding()] attribute will automatically add Common Parameters (-Verbose, -Debug, -ErrorAction etc.) to your function.

    By splatting $PSBoundParameters to the inner call, you automatically pass these options directly to Remove-ItemProperty

    The functions throws an error if the item (registry key) has no properties (Get-Member complains about a missing object).

    The above approach won't have that problem