如何删除注册表值,其中注册表值如'*字符串*';使用PowerShell?
PowerShell正在将注册表值(及其注册表数据)作为数组显示在其父注册表项的如何删除注册表值,其中注册表值如'*字符串*';使用PowerShell?,powershell,matching,dword,Powershell,Matching,Dword,PowerShell正在将注册表值(及其注册表数据)作为数组显示在其父注册表项的$\\属性中。如何仅选择和删除值类似于\*String\*的注册表值 我尝试过在StackOverflow和Google上进行上下搜索,尝试了许多组合,包括获取项目、获取项目属性、获取子项目、选择对象(包括-ExpandProperty参数)和Where Object来选择我想要的注册表值(在继续合并删除之前)。我现在完全不知所措,不知道如何简单地查找特定注册表项中存在的注册表值,如\*Text\*,并将其删除。如此
$\\属性中。如何仅选择和删除值类似于\*String\*
的注册表值
我尝试过在StackOverflow和Google上进行上下搜索,尝试了许多组合,包括获取项目
、获取项目属性
、获取子项目
、选择对象
(包括-ExpandProperty
参数)和Where Object
来选择我想要的注册表值(在继续合并删除之前)。我现在完全不知所措,不知道如何简单地查找特定注册表项中存在的注册表值,如\*Text\*
,并将其删除。如此简单的事情似乎非常困难!我不知道如何处理数组中的数据
Get-Item -Path HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs
每个注册表值都作为数组的一部分列在注册表项HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\sharedlls
的$\\属性下。注册表值-如“\*Text\*”;删除项
删除注册表值匹配\*文本\*
为了澄清以确保某些词汇表的正确技术用法,“注册表项”显示为regedit.exe中的文件夹
“注册表值”是任何类型的条目,如REG\\u SZ
、REG\\u DWORD
、REG\\u MZ
,等等。注册表值包含“注册表数据”,根据注册表类型,它可能是字符串、32位值(有时表示为1、0或0x000000、0x000001)
我们通常将“注册表值”称为“注册表项”(这是不正确的技术用法),“注册表数据”称为“注册表值”(也是不正确的技术用法),“注册表项”称为文件夹/位置/“注册表中的此位置”。仅按值名称筛选删除注册表值:
更新,基于您自己的简化:
# The target registry key's full path.
$keyPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs'
# Pass the value name pattern to Remove-ItemProperty's -Name parameter.
# Remove `-WhatIf` if the preview suggests that the operation works as intended.
Remove-ItemProperty -Path $keyPath -Name *Text* -WhatIf
陷阱:如果要将通配符表达式(如*Text*
与-Name
一起使用,则必须将其与-Path
而不是-LiteralPath
组合,即使密钥路径本身不是通配符;如果使用
-LiteralPath
,-Name
,也会按字面意思(逐字)进行
如果确实需要使用-LiteralPath
(例如,如果LiteralPath包含*
字符,例如在HKEY\U CLASSES\U ROOT\*
):
注意使用Get Item
而不是Get ItemProperty
[1]
Get Item
返回一个对象,该对象表示类型为[Microsoft.Win32.RegistryKey]
的整个密钥,PowerShell用一个.Property
注释属性装饰该属性,该属性包含所有密钥值名称的数组
重要:在.Property
数组中,PowerShell将默认值名称转换为名称(默认)
,默认值名称是API级别的空字符串('
)
将运算符-like
应用于数组LHS使其充当仅返回匹配项子数组的筛选器
Remove ItemProperty
的-Name
参数直接接受要从目标键中删除的属性(注册表值)名称数组
按值名称和/或数据筛选器删除值:
注意:由于使用了-PipelineVariable
公共参数,此解决方案需要PSv4+
# The target registry key's full path.
$keyPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs'
$pattern = '*Text*'
# Look for $pattern in both the name and the data.
# Remove `-WhatIf` if the preview suggests that the operation works as intended.
Get-Item -LiteralPath $keyPath -PipelineVariable key |
ForEach-Object Property |
Where-Object {
$valueName = ($_, '')[$_ -eq '(default)'] # translate '(default)' to '' for API
$valueName -like $pattern -or $key.GetValue($valueName) -like $pattern
} |
Remove-ItemProperty -LiteralPath $keyPath -WhatIf
-PipelineVariable key
存储变量$key
中的获取项返回的[Microsoft.Win32.RegistryKey]
实例,以供以后在管道中使用
ForEach对象属性
枚举目标键的值名称(通过.Property
注意:PowerShell属性添加到输出[Microsoft.Win32.RegistryKey]
实例,如前所述)
- 在
Where Object
脚本块中,$\ucode>然后引用手头的值名称,并使用$key.GetValue()
检索相关数据
- 重要:在
.Property
数组中,PowerShell将默认值名称转换为名称(默认)
;因此,如果$
为(默认)“
,在调用$.GetValue()
之前,必须将其转换为”
,这就是
($”)[$\uEQ(默认值)]
所做的
- 然后,与条件匹配的任何值名称都将通过管道传输到
Remove ItemProperty
,从而将这些名称隐式绑定到其-Name
参数
如果只想列出匹配值及其数据,请参阅
[1] Get-ItemProperty-Path$keyPath-Name*Text*
技术上也可以,但输出是[pscustomobject]类型的单个对象
,您必须通过反射枚举其属性,因为属性名称反映匹配的值名称;而通过.psobject.properties执行此操作时,如works中所示,也允许您按数据进行筛选,缺点是PowerShell的注册表提供程序会自动将其自己的属性添加到属性集合中即PSPath
,PsParentPath
,PSChildName
,PSDrive
,PSProvider
,这意味着按名称筛选的通配符表达式可能会意外地包含它们,或者更糟糕的是,如果键上碰巧存在同名值(尽管可能性不大),则提供程序属性将覆盖
# The target registry key's full path.
$keyPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs'
$pattern = '*Text*'
# Look for $pattern in both the name and the data.
# Remove `-WhatIf` if the preview suggests that the operation works as intended.
Get-Item -LiteralPath $keyPath -PipelineVariable key |
ForEach-Object Property |
Where-Object {
$valueName = ($_, '')[$_ -eq '(default)'] # translate '(default)' to '' for API
$valueName -like $pattern -or $key.GetValue($valueName) -like $pattern
} |
Remove-ItemProperty -LiteralPath $keyPath -WhatIf
# get-itemproperty2.ps1
# get-childitem skips top level key, use get-item
# can't remove default name
param([parameter(ValueFromPipeline)]$key)
process {
$valuenames = $key.getvaluenames()
if ($valuenames) {
$valuenames | foreach {
$value = $_
[pscustomobject] @{
Path = $key -replace 'HKEY_CURRENT_USER',
'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
Name = $Value
Value = $Key.GetValue($Value)
Type = $Key.GetValueKind($Value)
}
}
} else {
[pscustomobject] @{
Path = $key -replace 'HKEY_CURRENT_USER',
'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
Name = ''
Value = ''
Type = ''
}
}
}
PS C:\users\me> get-item hkcu:\key1 | get-itemproperty2
Path Name Value Type
---- ---- ----- ----
HKCU:\key1 name1 value1 String
HKCU:\key1 name2 value2 String
PS C:\users\me> get-item hkcu:\key1 | get-itemproperty2 | where name -eq name1
Path Name Value Type
---- ---- ----- ----
HKCU:\key1 name1 value1 String
PS C:\users\me> get-item hkcu:\key1 | get-itemproperty2 | where name -eq name1 | Remove-ItemProperty -whatif
What if: Performing the operation "Remove Property" on target "Item: HKEY_CURRENT_USER\key1 Property: name1".
PS C:\users\me> get-childitem -recurse hkcu:\key1 | get-itemproperty2
Path Name Value Type
---- ---- ----- ----
HKCU:\key1\key2 name2 value2 String
HKCU:\key1\key2 default String
HKCU:\key1\key2\key3 name3 value3 String
HKCU:\key1\key2\key3\key4