如何使用PowerShell删除REG_DWORD注册表值(如“ * String *”的注册表值)?

时间:2019-04-22 11:53:13

标签: powershell matching dword

PowerShell在其父注册表项中的 @Override public void onMessageReceived(RemoteMessage remoteMessage) { // Create and show notification String strForResponse = remoteMessage.getData().toString(); Map<String, String> data = remoteMessage.getData(); strForResponse = strForResponse.substring(1, strForResponse.length() - 1); //remove curly brackets String[] keyValuePairs = strForResponse.split(","); //split the string to creat key-value pairs Map<String, String> map = new HashMap<>(); for (String pair : keyValuePairs) //iterate over the pairs { String[] entry = pair.split("="); //split the pairs to get key and value map.put(entry[0].trim(), entry[1].trim()); //add them to the hashmap and trim whitespaces try { JSONObject obj = new JSONObject(map); System.out.println("Firebase Notification Responce:" + obj.toString()); destination = obj.getString("destination"); source = obj.getString("source"); source_override = obj.getString("source_override"); } catch (Throwable t) { } } Intent intent = new Intent(MyFirebaseMessagingService.this,"Class Name".class); intent.putExtra("destination", destination); intent.putExtra("source", source); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } 中以数组形式显示注册表值(及其注册表数据)。如何仅选择和删除值类似于$\_.Property的注册表值?

我尝试过在StackOverflow上下搜索,并尝试在Google上尝试许多涉及\*String\*Get-ItemGet-ItemPropertyGet-ChildItem(包括Select-Object参数),然后-ExpandProperty选择我想要的注册表值(在继续进行合并删除之前)。现在,我完全不知所措,无法找出如何简单地找到特定注册表项中存在的注册表值(如Where-Object)并将其删除。如此简单的事情似乎是如此困难!我不知道如何处理数组中的数据!

\*Text\*

每个注册表值都被列为注册表项Get-Item -Path HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs $\_.Property下的数组的一部分。我期待的结果是,我可以做类似HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs的操作来删除匹配$\_.RegistryValue -Like '\*Text\*' | Remove-Item的注册表值。

为明确起见,以确保正确使用某些词汇,在regedit.exe中,“注册表项”作为文件夹出现。

“注册表值”是任何类型的条目,例如\*Text\*REG\_SZREG\_DWORD等。注册表值包含“注册表数据”,并且根据注册表类型的不同,它可以是字符串,32位值(有时表示为1、0或0x000000、0x000001)。

我们经常将“注册表值”称为“注册表项”(这是不正确的技术用法),将“注册表数据”称为“注册表值”(也是不正确的技术用法),而将“注册表项”称为文件夹/位置/“注册表中的此位置”。

2 个答案:

答案 0 :(得分:0)

仅通过值名称过滤器删除注册表值:

更新,基于您自己的简化:

# 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这样的通配符表达式,则必须将其与{{1} }而不是-Path,即使密钥路径本身不是通配符;与
-LiteralPath一起使用,-LiteralPath也照原样被采用(普通)。

如果您确实需要使用-Name(例如,文字路径包含-LiteralPath字符,例如*中的字符):

HKEY_CLASSES_ROOT\*

请注意使用# The target registry key's full path. $keyPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\SharedDLLs' # Get an array of all value names that match the name wildcard pattern. $valueNames = (Get-Item -LiteralPath $keyPath).Property -like '*Text*' # Pass the array of value names to Remove-ItemProperty's -Name parameter. # Remove `-WhatIf` if the preview suggests that the operation works as intended. if ($valueNames) { Remove-ItemProperty -LiteralPath $keyPath -Name $valueNames -WhatIf } 而不是Get-Item [1]

Get-ItemProperty返回一个对象,该对象代表类型为Get-Item的整个键,PowerShell用[Microsoft.Win32.RegistryKey] note属性修饰该对象,该属性包含所有键的值名称的数组。

重要:在.Property数组中,PowerShell转换默认值名称,它是空字符串({{ 1}}),将其命名为.Property

将运算符''应用于 array LHS使其充当 filter ,仅返回匹配项的子数组。

'(default)'的{​​{1}}参数直接接受要从目标键中删除的属性(注册表值)名称的 array


通过值名称和/或数据过滤器删除值:

注意:由于使用了-like公共参数,因此该解决方案需要PSv4 +。

Remove-ItemProperty
  • -Name-PipelineVariable返回的# 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中,以供以后在管道中使用。

  • [Microsoft.Win32.RegistryKey]枚举目标键的值名称(如前所述,通过PowerShell添加到输出Get-Item实例的$key笔记属性)。

  • ForEach-Object Property脚本块中,.Property引用了手边的值名称,并且[Microsoft.Win32.RegistryKey]用于检索关联的数据。

    • 重要:在Where-Object数组中,PowerShell转换默认值名称,它是空字符串({{ 1}})在API级别上,命名为$_;因此,如果$key.GetValue(<valueName>).Property,则必须在调用''之前将其转换为'(default)',这是
      $_的作用。
  • 然后,将与值匹配的所有值名称通过管道传递到'(default)',后者将这些名称隐式绑定到其''参数。

如果您只想 列出匹配值及其数据,请参见this answer


[1] $_.GetValue(<valueName>)在技术上也可以使用,但是输出是类型为($_, '')[$_ -eq '(default)']单个对象,您必须通过以下方法枚举其属性反射,因为属性名称反映了匹配的值名称;尽管this answer中通过Remove-ItemProperty进行的操作有效,并且允许您也通过 data 进行过滤,但陷阱是PowerShell的注册表提供程序会自动将其自身的属性添加到属性,即-NameGet-ItemProperty -Path $keyPath -Name *Text*[pscustomobject].psobject.propertiesPSPath,这意味着按名称筛选的通配符表达式可能会意外地包含它们,或者更糟的是,如果键上确实存在相同名称的值(即使不太可能),则提供程序属性会覆盖它们。

答案 1 :(得分:0)

注册表提供者非常糟糕。我只希望get-itemproperty像这样工作:

# 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
相关问题