Powershell:删除所有包含字符串的注册表项

标签 powershell registry

我想删除所有包含 1000+ 的键( Python35 )来自:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components
例如,我想删除与该键类似的所有键:

  • 键名:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components\0027CAECCC428F356B8D845FF8331246
  • 姓名:0F617A7B1C879BC47865E0155CDD6722
  • 数据:C:\Users\Me\AppData\Local\Programs\Python\Python35\Lib\venv\__init__.py

  • 我试过这个。
    Get-ChildItem -path HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components\ -Recurse | where { $_.Name -match 'Python35'} | Remove-Item -Force
    

    Powershell 运行没有错误,但是当我在注册表中检查它时, key 仍然存在。

    Powershell 以管理员身份运行,管理员拥有 key HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components 的所有权。并完全控制该键及其子键。

    最佳答案

    尝试以下脚本:

    $RE = 'Python35'
    $Key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components'
    Get-ChildItem $Key -Rec -EA SilentlyContinue | ForEach-Object {
       $CurrentKey = (Get-ItemProperty -Path $_.PsPath)
       If ($CurrentKey -match $RE){
         $CurrentKey|Remove-Item -Force -Whatif
       }
    }
    
    如果输出看起来正常,请删除 -WhatIf来自 Remove-Item 的参数

    关于Powershell:删除所有包含字符串的注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44196938/

    相关文章:

    c++ - 检测键是否是加载/安装的注册表配置单元的根

    architecture - 如何通过WMI确定操作系统平台?

    windows - 取消 RegNotifyChangeKeyValue 的正确方法是什么?

    permissions - 具有完全权限的注册表

    arrays - PowerShell Poker Hand - 构建数组的问题

    xml - 在此对象上找不到属性 '...',在 powershell 中设置 xml 值失败

    c# - Windows 注册表中的 ProductName,是否针对不同语言进行了本地化?

    powershell - 为什么 PowerShell 无法发送正确的 Crumb?

    iis - 如何通过 WMI 使用网络共享创建 IIS 虚拟目录

    regex - 在 Powershell 中插入数字替换