Powershell:删除所有包含字符串的注册表项 [英] Powershell: delete all the registry keys containing a string
问题描述
我想从以下位置删除所有包含 Python35
的键 (1000+
):HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\组件
I would like to delete all the keys (1000+
) containing Python35
from :HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components
例如我想删除所有与那个相似的键:
For instance I would like to delete all the keys similar to that one:
键名:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components\0027CAECCC428F356B8D845FF8331246
名称:0F617A7B1C879BC47865E0155CDD6722
我试过了.
Get-ChildItem -path HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components\ -Recurse | where { $_.Name -match 'Python35'} | Remove-Item -Force
Powershell 运行没有错误,但是当我在注册表中检查它时,密钥仍然存在.
Powershell runs without error,but when I check it in the registry, the keys are still there.
Powershell 以管理员身份运行,管理员拥有密钥 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components
的所有权,并且还拥有完全控制权在该键及其子键上.
Powershell is run as admin and Admin has the ownership of the key HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components
and also full control on that key and its subkeys.
推荐答案
试试下面的脚本:
$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
}
}
如果输出看起来没问题,从 Remove-Item
If the output looks OK remove the -WhatIf
paramter from Remove-Item
这篇关于Powershell:删除所有包含字符串的注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!