- $delete?='abc'
- dir Registry::HKEY_LOCAL_MACHINE\SOFTWARE\XYZ -Recurse -ErrorAction SilentlyContinue|%{
- $Name=$_.Name
- if([regex]::Matches($Name,'(?!.+\\).+$').value -match $delete?){
- try {
- del Registry::$Name -WhatIf -Recurse -Force -ErrorAction SilentlyContinue
- if(!$?){throw}
- }catch{
- Write-Warning "目标删除失败: $Name"
- }
- }
- Get-ItemProperty Registry::$Name -Name $delete? -ErrorAction SilentlyContinue|%{
- $Name=$_.Name
- try {
- Remove-ItemProperty Registry::$Name $delete? -WhatIf
- if(!$?){throw}
- }catch{
- Write-Warning "目标删除失败: $Name"
- }
- }
- }
复制代码 没有实物测试,所以凭感觉写的,删除可能会比较久,因为注册表的数据量实在太大了 去掉whatif可以执行真正的删除,不去掉则只是测试结果 |