简体   繁体   English

Powershell - 删除(默认)注册表项?

[英]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? 有关如何通过PowerShell删除(默认)注册表项的任何想法?

Have you try to delete it from the registry .msc console? 您是否尝试从注册表.msc控制台中删除它?

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. 这是通过PowerShell从注册表项中删除默认值:( 默认)的答案。

It's supposedly a bug in PS, since v1. 从v1开始,它应该是PS中的一个错误。 Now as of 2018, with v5.1, bug is still present. 现在截至2018年,随着v5.1,bug仍然存在。
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, 实际上Remove-Item -Path 'Registry::HKEY_CURRENT_USER\\Software\\Testing\\' -name '(Default)'是错误的,因为(默认)不是Key,而是属性,
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. 幸运的是,与许多PS Cmdlet一样, Remove-ItemProperty的功能可以通过使用Method来代替。 In this case DeleteValue() Method invoked on a RegistryKey Object. 在这种情况下,在RegistryKey对象上调用DeleteValue()方法。
There is a small hiccup, as a Key, obtained by Get-Item is Read-only. 由Get-Item获得的一个小打嗝作为Key是只读的。 But another Method, OpenSubKey() can be used to get writable access to a Key. 但另一个方法,OpenSubKey()可用于获取对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 SupportsShouldProcess - 允许-WhatIf参数
  • Param ... etc - to allow pipeline use, both String and RegistryKey inputs and for sanity check Param ...等 - 允许管道使用,String和RegistryKey输入以及健全性检查
  • $ParentKey - get parent key on which a method is used to open original key in writable mode $ ParentKey - 获取父键,在该父键上使用方法以可写模式打开原始键
  • $KeyName - String value of the original Key name without path $ KeyName - 没有路径的原始密钥名称的字符串值
  • OpenSubKey() - will get a child Key of a Key, also offers second argument to choose write access OpenSubKey() - 将获得一个Key Key of Key,还提供第二个参数来选择写访问权限
  • DeleteValue() with an empty string as an argument. DeleteValue(),以空字符串作为参数。 Inside of the RegistryKey Object's methods, (Default) value is referenced by an empty string. 在RegistryKey对象的方法内部,(默认)值由空字符串引用。

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. 在OP可能被问及与名称(默认)从关键,因为人们在这里登陆大多搜索删除密钥 ,而不是默认值。 In that case a -LiteralPath should be used, because Brackets are considered special characters in -Path argument, whilst -LiteralPath takes their literal meaning: 在这种情况下,应该使用-LiteralPath,因为在-Path参数中将括号视为特殊字符,而-LiteralPath则使用它们的字面含义:

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: 或者最后,如果Path不是文字,则可以使用函数来转义字符串,在编写代码时不知道:

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 (*)这将在Path参数中添加具有特殊含义的字符前面的`

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 迭代@ papo的答案 - 如果OP希望清除(取消) (Default)注册表值的内容并使用标准方式在PowerShell中访问注册表,oneliner将是

$(Get-Item -Path "HKCU:\Software\Testing").OpenSubKey("", $true).DeleteValue("")
  • Get-Item -Path fetches the RegistryKey object for reading Get-Item -Path获取RegistryKey对象以供读取
  • OpenSubKey("", $true) reopens the same key with write permissions OpenSubKey("", $true)使用写入权限重新打开相同的密钥
  • DeleteValue("") clears the default (in .NET treated as unnamed) value DeleteValue("")清除默认值(在.NET中视为未命名)值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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