Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 806
Kyle Avatar asked Oct 12 '12 12:10

Kyle


2 Answers

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

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

like image 173
CB. Avatar answered Oct 14 '22 01:10

CB.


(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

like image 37
papo Avatar answered Oct 14 '22 00:10

papo