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('')
}
Use:Remove-ItemPropertyDefault Registry::HKEY_CURRENT_USER\Software\Testing
orGet-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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With