Marina Kaygorodova
Marina Kaygorodova

Reputation: 13

Batch remove full registry key using for loop

I can find registry keys using:

REG Query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData /F "Need removed manually Add-In" /S

But this result contains subkeys:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\145BA990C18DA984499E1D1F9A1BD64D
    BB02C99E89C99CD4A8CC6A7AA7576194    REG_SZ    C:\Program Files (x86)\Need removed manually Add-In\SimonFell\PocketSoap\psDime.dll

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\37AC9AD16C5204A4D9D92967A6699DEE
    BB02C99E89C99CD4A8CC6A7AA7576194    REG_SZ    C:\Program Files (x86)\Need removed manually Add-In\SimonFell\PocketSoap\pSOAP32.dll

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\DFD1C740E6047204C8387A02138B234A
    BB02C99E89C99CD4A8CC6A7AA7576194    REG_SZ    C:\Program Files (x86)\Need removed manually Add-In\SimonFell\PocketSoap\pocketHTTP.dll

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\F3A3DDBA307ACB945868E32C44414A5E
    BB02C99E89C99CD4A8CC6A7AA7576194    REG_SZ    C:\Program Files (x86)\Need removed manually Add-In\SimonFell\PocketSoap\psProxy.dll

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\BB02C99E89C99CD4A8CC6A7AA7576194\InstallProperties
    DisplayName    REG_SZ    Need removed manually Add-In

End of search: 5 match(es) found.

When I try to delete using with a script

FOR /f "delims=" %%a IN ('REG Query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData /F "Need removed manually Add-In" /S') DO REG delete "%%a" /f

I delete first full key successful but after I that, I get an error because the script is trying to delete the second line with string detail:

$ REG delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\145BA990C18DA984499E1D1F9A1BD64D" /f
The operation completed successfully.

$ REG delete "    BB02C99E89C99CD4A8CC6A7AA7576194    REG_SZ    C:\Program Files (x86)\Need removed manually Add-In\SimonFell\PocketSoap\psDime.dll" /f
ERROR: Invalid key name.
Type "REG DELETE /?" for usage.

How can I delete only the actual keys and avoid this error?

Upvotes: 1

Views: 1536

Answers (2)

Compo
Compo

Reputation: 38613

…belated answer.

I have included this because the Reg Query is searching, /F in Data values, /D and it's probably safe to just exclude all lines not including the string HKEY_.

@Echo Off
Set "Key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData"
Set "Str=Need removed manually Add-In"
For /F "Delims=" %%A In ('REG Query "%Key%" /S /F "%Str%" /D^|Find "HKEY_"'
) Do Echo=Reg Delete "%%A" /F
Pause

If you're happy with the output, simply remove Echo= from the penultimate line and optionally remove the last line.

Upvotes: 0

user7818749
user7818749

Reputation:

You can incorporate findstr in your query and exclude using /V the word REG_SZ as well as the search string completion message.

@echo off
for /f "delims=" %%a IN ('REG Query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData /F "Need removed manually Add-In" /S ^| findstr /V "REG_SZ" ^| findstr /V "End of Search"') DO echo REG delete "%%a" /f

Here I echo the delete command, so you can test it first. Once happy, simply remove the echo from this part echo REG delete "%%a" /f

Upvotes: 1

Related Questions