Powershell: delete all the registry keys containing a string

MagTun

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:

  • Keyname: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-2\Components\0027CAECCC428F356B8D845FF8331246

  • Name: 0F617A7B1C879BC47865E0155CDD6722

  • Data: C:\Users\Me\AppData\Local\Programs\Python\Python35\Lib\venv\__init__.py

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.

user6811411

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sublime - delete all lines containing specific value

Redis delete all keys except keys that start with

Use PowerShell to search for string in registry keys and values

Delete all lines not containing string in Sublime

Do not delete all the keys of the localstorage

ex/importing multivalue registry keys in PowerShell via XML

Delete directories with files containing specific string in PowerShell

How to delete REG_DWORD registry value where registry value like '*String*' using PowerShell?

Unable to send keys string containing acute character

Create multiple (sub) registry keys in powershell

Editing and Viewing Registry Keys from Powershell

Editing registry keys remotely with Powershell

Delete all lines in all files in cPanel containing a string

Delete files containing string

Powershell - Delete folders containing a file with a specific FileDescription?

How to iterate all of the registry keys?

How to write a string containing ascii codes into the Registry?

Delete string containing some words

how to delete all lines with a particular string but not deleting the lines containing the same string in a particular section of the same file

sed : delete all lines NOT containing string A or B, starting at 2nd line

Batch script to find and delete registry keys and/or values

Powershell to find registry key and delete it

How to Delete All Registry Keys without Being Stopped by One Undeletable Key?

Set all registry keys with one PowerShell function

How to read registry keys to output in powershell?

Getting software version using registry keys in PowerShell

Find and display registry empty keys with Powershell

How to construct a string containing the keys of a map in Terraform?

Powershell Registry String to Workable Date