简体   繁体   中英

Powershell - Remove (Default) registry key?

I see you can remove items by running:

Remove-Item -Path hkcu:\CurrentVersion

But I tried

Remove-Item -Path 'Registry::HKEY_CURRENT_USER\Software\Testing\(Default)'

But that did not work. I also tried

Remove-Item -Path 'Registry::HKEY_CURRENT_USER\Software\Testing\' -name '(Default)'

Which also did not work. Any ideas how I can remove a (Default) registry key via powershell?

Have you try to delete it from the registry .msc console?

I think isn't possible to delete the default value for a key.

(Below line is an answer for removing (Default) as a Key name),
This is the answer for removing default Value: (Default) from a registry Key by PowerShell.

It's supposedly a bug in PS, since v1. Now as of 2018, with v5.1, bug is still present.
Actually Remove-Item -Path 'Registry::HKEY_CURRENT_USER\\Software\\Testing\\' -name '(Default)' is wrong for the reason that (Default) is not a Key, but a Property,
but let's say most of us tried:
Remove-ItemProperty -Path Registry::HKEY_CURRENT_USER\\Software\\Testing -Name '(Default)'
and this should have worked.

Luckily, as with many PS Cmdlets, function of Remove-ItemProperty could be substituted by use of a Method. In this case DeleteValue() Method invoked on a RegistryKey Object.
There is a small hiccup, as a Key, obtained by Get-Item is Read-only. But another Method, OpenSubKey() can be used to get writable access to a Key.

function Remove-ItemPropertyDefault {
  [CmdletBinding(SupportsShouldProcess=$true)]
  Param (
    [parameter(ParameterSetName='Path', Mandatory=$true, Position=0)]
    [String]$Path,
    [parameter(ParameterSetName='Key', Mandatory=$true, ValueFromPipeline=$true)]
    [Microsoft.Win32.RegistryKey]$Key
  )
    if ($Path) {$Key = Get-Item -LiteralPath $Path}
    $ParentKey = Get-Item -LiteralPath $Key.PSParentPath
    $KeyName = $Key.PSChildName
    ($ParentKey.OpenSubKey($KeyName, $True)).DeleteValue('')
}
  • SupportsShouldProcess - to allow -WhatIf argument
  • Param ... etc - to allow pipeline use, both String and RegistryKey inputs and for sanity check
  • $ParentKey - get parent key on which a method is used to open original key in writable mode
  • $KeyName - String value of the original Key name without path
  • OpenSubKey() - will get a child Key of a Key, also offers second argument to choose write access
  • DeleteValue() with an empty string as an argument. Inside of the RegistryKey Object's methods, (Default) value is referenced by an empty string.

Use:
Remove-ItemPropertyDefault Registry::HKEY_CURRENT_USER\\Software\\Testing
or
Get-Item Registry::HKEY_CURRENT_USER\\Software\\Testing | Remove-ItemPropertyDefault
or
$k = Get-Item Registry::HKEY_CURRENT_USER\\Software\\Testing Remove-ItemPropertyDefault -Key $k
Of course if you don't need or want a function, just take those three lines from it and use it in your code.
This is a nice article which gave me this idea.


The OP possibly asked about removing a Key with the name (Default) , not a Default value from a key as people landing here mostly searched for. In that case a -LiteralPath should be used, because Brackets are considered special characters in -Path argument, whilst -LiteralPath takes their literal meaning:

Remove-Item -LiteralPath 'Registry::HKEY_CURRENT_USER\Software\Testing\(Default)'

or brackets needs to be escaped:

Remove-Item -Path "Registry::HKEY_CURRENT_USER\Software\Testing\`(Default`)"

(*) Single quote can't be used then, but double-quotes, or no quotes must be used for escape character to work.

Or lastly, if the Path is not literal, a function could be used to escape a string, not known at the time of writing the code:

Remove-Item -Path ([Management.Automation.WildcardPattern]::Escape('Registry::HKEY_CURRENT_USER\Software\Testing\(Default)'))

(*) this will add ` in front of characters with a special meaning in Path argument

Iterating on @papo's answer - If OP wished to clear (nullify) the contents of the (Default) registry value and use the standard way how registry are accessed in PowerShell, oneliner would be

$(Get-Item -Path "HKCU:\Software\Testing").OpenSubKey("", $true).DeleteValue("")
  • Get-Item -Path fetches the RegistryKey object for reading
  • OpenSubKey("", $true) reopens the same key with write permissions
  • DeleteValue("") clears the default (in .NET treated as unnamed) value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM