MagTun
MagTun

Reputation: 6205

Powershell: delete all the registry keys containing a string

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:

I tried this.

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

Powershell runs without error,but when I check it in the registry, the keys are still there.

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.

Upvotes: 6

Views: 19143

Answers (1)

user6811411
user6811411

Reputation:

Try the following script:

$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
   }
}

If the output looks OK remove the -WhatIf paramter from Remove-Item

Upvotes: 9

Related Questions