4

I have a list of registry keys/values/entries that I would like to delete (basically the list in #3 below without the REG DELETE stuff). It works as is but I'd like to have the batch...

I. Write the listfile.txt itself if list.txt doesn't exist in %~dp0

Example:

set "ListFile=%TEMP%\ListFile.txt"
set "List=%~dp0list.txt"

if not exist "%List%" (
    (
        echo Some regisrtry key
        echo Some registry value
        echo Some registry subkey
        echo No idea how it should all be written like
    ) >"%ListFile%"
)

II. Check the existence of each in the list with REG QUERY and have an ERRORLEVEL

If it exists..

  1. REG EXPORT it to its own valid .reg (that can be imported back in case something goes wrong).

Example:

for /f %%A in (%src%) do (
    if not exist "%bkp%\%%A" md "%bkp%\%%A"
    reg export "%%A" "%bkp%\%%A\key.reg"

I'm not sure if the above will work with specific values or all entries within a specified key.

  1. compare the list and .reg

  2. if it matches, run REG DELETE /f or REG DELETE /v /f or REG DELETE /va /f as appropriate.

REG DELETE "HKCU\Software\Google\Chrome" /f
REG DELETE "HKLM\Software\Google\Chrome" /f
REG DELETE "HKCU\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Update" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Enrollment" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Update\ClientState\{430FD4D0-B729-4F61-AA34-91526481799D}" /v CloudManagementEnrollmentToken /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Policies" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\Policies" /f
REG DELETE "HKCU\Software\Policies" /f
REG DELETE "HKLM\Software\Policies" /f
REG DELETE "HKLM\Software\Policies\Microsoft\Windows Defender" /v DisableAntiSpyware /f
REG DELETE "HKLM\Software\Policies\Microsoft\Edge" /va /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f
  1. ECHO to cmd and %~dp0output.log that the keys/values/entries had been processed and deleted

If it doesn't exist..

  1. ECHO to cmd and %~dp0output.log that it doesn't exist
"HKCU\Software\Google\Chrome" key does not exist
No entries can be found in "HKLM\Software\Policies\Microsoft\Edge"
Value DisableAntiSpyware cannot be found in "HKLM\Software\Policies\Microsoft\Windows Defender"

III. Delete the list that was created by the batch but not if it was already present.

IF EXIST "%ListFile%" ( DEL "%ListFile%" ) ELSE ( goto :eof )

I attempted to modify the snippets I found but do not understand yet how for loops work at all and also in a way that I will only have one list and the for loop will adapt in case it is a key, is a value (/v) and/or only requires to process all entries within the specified key (/va)

The current script below works so far but is misssing all the points above. Any help to add those is appreciated.

REG DELETE "HKCU\Software\Google\Chrome" /f
REG DELETE "HKLM\Software\Google\Chrome" /f
REG DELETE "HKCU\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Update" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Enrollment" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Update\ClientState\{430FD4D0-B729-4F61-AA34-91526481799D}" /v CloudManagementEnrollmentToken /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Policies" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\Policies" /f
REG DELETE "HKCU\Software\Policies" /f
REG DELETE "HKLM\Software\Policies" /f
REG DELETE "HKLM\Software\Policies\Microsoft\Windows Defender" /v DisableAntiSpyware /f
REG DELETE "HKLM\Software\Policies\Microsoft\Edge" /va /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f

@RD /S /Q "%ProgramFiles(x86)%\Google\Policies"
@RD /S /Q "%WinDir%\System32\GroupPolicy"
@RD /S /Q "%WinDir%\System32\GroupPolicyUsers"

ECHO.
gpupdate /force
ECHO.
ECHO Complete. Will shutdown..
pause>nul
shutdown.exe /r /t 00
EXIT /B 0
0

1 Answer 1

3
@echo off

for /f usebackq^tokens^=1-3*delims^=^" %%i in =;(
    `type "%~f0" ^| findstr /b \^"`
        );= do if "%%~l" == "" 2>nul =;(
             reg query "%%~i" >nul && =;(
             reg export "%%~i" "%%~k.reg" /y
             reg delete "%%~i" /f
            );= || echo/Key not exist "%%~i"
        );= else if "%%~l" == "/va" 2>nul =;(
             reg query "%%~i" >nul && =;(
             reg export "%%~i" "%%~k.reg" /y
             reg delete "%%~i" /va /f
            );= || echo/Key not exist "%%~i"
        );= else 2>nul =;(
             reg query "%%~i" >nul && =;(
             reg export "%%~i" "%%~l.reg" /y
             reg delete "%%~i" /v "%%~j" /f
            );= || echo/Key not exist "%%~i"
        );=

>nul 2>&1 rd /s /q "%ProgramFiles(x86)%\Google\Policies" "%Windir%\System32\GroupPolicy" "%Windir%\System32\GroupPolicyUsers"

echo/ & gpupdate /force | findstr . 
timeout -1 | echo/Complete. Will shutdown!.. 

echo/ & shutdown.exe /r /t 00   
exit /b 0 || goto :eof

::--------------------------------------------------------------::
::         Your listing of registry keys/values/entries         ::
::--------------------------------------------------------------::

"HKCU\Software\Policies" "HKCU_Policies"
"HKCU\Software\Google\Chrome" "HKCU_Chrome"
"HKCU\Software\Policies\Google\Chrome" "HKCU_Google_Chrome"
"HKCU\Software\Microsoft\WindowsSelfHost" "HKCU_Microsoft_WindowsSelfHost"
"HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" "HKCU_Microsoft_Policies"

"HKLM\Software\Policies" "HKLM_Policies"
"HKLM\Software\Google\Chrome" "HKLM_Chrome"
"HKLM\Software\Microsoft\Policies" "HKLM_Microsoft_Policies"
"HKLM\Software\Policies\Google\Chrome" "HKLM_Google_Chrome"
"HKLM\Software\Policies\Google\Update" "HKLM_Google_Update"
"HKLM\Software\Microsoft\WindowsSelfHost" "HKLM_Microsoft_WindowsSelfHost"
"HKLM\Software\Policies\Microsoft\Edge" "HKLM_Edge" /va

"HKLM\Software\Microsoft\Windows\CurrentVersion\Policies" "HKLM_CurrentVersion_Policies"
"HKLM\Software\Policies\Microsoft\Windows Defender" "HKLM_Windows_Defender" "DisableAntiSpyware"
"HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" "HKLM_WindowsStore_WindowsUpdate"

"HKLM\Software\WOW6432Node\Google\Enrollment" "HKLM_WOW6432Node_Google_Enrollment"
"HKLM\Software\WOW6432Node\Microsoft\Policies" "HKLM_WOW6432Node_Microsoft_Policies"
"HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" "HKLM_WOW6432Node_CurrentVersion_Policies"
"HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" "HKLM_WOW6432Node_WindowsStore_WindowsUpdate"
"HKLM\Software\WOW6432Node\Google\Update\ClientState\{430FD4D0-B729-4F61-AA34-91526481799D}" "HKLM_WOW6432Node_Google_Update_ClientState" "CloudManagementEnrollmentToken"

1. Use your batch store to list the keys and names (uniques) to files.reg

2. Use a For /F loop to find/filter the keys

3. Choose to work with "quotes" as delimiter in this For /F loop

4. In the looping command, filter out the lines that start with " (quote) with:

  • for ...('
             type "%~f0" ^|findstr /b \^" 
                                          ')do...
    

5. Use occurrences of value (null, equal or different) in %%l in an if to handle each case

6. Using && operator for reg query success tells you if key/value exists in the registry

6. Using && operator for reg query success tells you if key/value exists in the registry


Obs. 1: You can remove multiple folders in one command:

   rd /q /s "FOLDER_01" "FOLDER_02" "FOLDER_nn"
rmdir /q /s "FOLDER_01" "FOLDER_02" "FOLDER_nn"

Obs. 2: You can remove a folder that does not exist, it does not return a nuclear cosmic error, it only returns a warning that the "the system cannot find", so you can just omit the possible simple text message when deleting the folder if it exists or not and it won't even abort the execution of your bat:

2>nul rmdir /q /s "FOLDER_01" "FOLDER_02" "FOLDER_nn"
if exist "FOLDER_01" rd /q /s "FOLDER_01"
if exist "FOLDER_02" rd /q /s "FOLDER_02"
if exist "FOLDER_nn" rd /q /s "FOLDER_nn"

Obs. 3: The same applies to the delete command:

2>nul del /q "FILE_01" "FILE_02" "FILE_nn"
if exist "FILE_01" del /q "FILE_01"
if exist "FILE_02" del /q "FILE_02"
if exist "FILE_nn" del /q "FILE_nn"

Additional resources:

6
  • That's pretty neat having the self-contained list in the batch script itself and controlling the logic to not execute it but let other parts of the command get that for things to process. I never got that sophisticated with batch but when I was heavy in batch, that would have indeed come in handy keeping things all in one place. I think it's this part doing that magic, right 'type "%~f0" ^| findstr /b \^" ' to build a list of those lines but I don't fully understand it 100% but that's very cool and nice!! Commented Jul 16, 2023 at 0:01
  • 1
    @VomitIT-ChunkyMessStyle yes: findstr /begin /with /literal (")
    – Io-oI
    Commented Jul 16, 2023 at 2:12
  • This is bad @$$. +1 I would also point out to OP that Powershell deals with registry stuff just like the file system. I hate when people say "just use powershell" when that isn't what OP asked for (and I love batch too) but needed to point out that this is probably a better tool for this job. Commented Jul 16, 2023 at 3:25
  • I too hate when people say "just use PowerShell" & then don't explain why. I used to be batch only then once I started using PowerShell, its just more robust, can do most things batch can do with less lines or don't have code fluky gotchas, and can definitely do things batch cannot more easily. I'm not against batch because that's what I grew up on in my IT sysadmin career, but if I personally tell someone here to 'just use powershell' I usually put context around the "why" part too and I don't just say "use Powershell because it's better, more modern, etc." if I write an answer @SeñorCMasMas Commented Jul 16, 2023 at 14:25
  • @Io-oI It's working but missing a few points such as exporting the keys, values and entries to their own .reg (goes without saying that it should be importable) and the errorlevel/logging. I did try to incorporate them but failed at it miserably. Please check: pastebin.com/v3VUPYY8 To SeñorCMasMas and VomitIT-ChunkyMessStyle Seen that a dozen times and yes, I am in agreement on hating to see recommendation when something else is being asked for, without offering an explanation, an alternative and the direct answer to what is being asked. Will eventually learn PowerShell.
    – vav
    Commented Jul 17, 2023 at 22:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .