Remove registry keys using reg.exe in a batch script

5

I've written this little batch script to help me auto-clean the registries of 300+ identical PC's of some very specific registry keys. It works right up to the point of passing the key variable to the "reg delete %1" command.

@echo off
C:
cd C:\Program Files\McAfee\Common Framework\
framepkg.exe remove=agent /silent

setlocal
for /F %%c in ('REG QUERY HKLM\SOFTWARE /s^|FIND "HKEY_"^|findstr /L /I /C:"mcafee"')  do call :delete %%c
endlocal
goto :EOF

:delete
reg delete /f %1
pause

Any and all debugging help would be extremely appreciated!

Lex

Posted 2012-09-25T15:02:19.007

Reputation: 53

2Cool, a script that deletes McAfee! This the payload of your malware or something? – allquixotic – 2012-09-25T15:06:03.290

1LOL NO!, also seriously. malware? its a batch script man. im a sys admin. the agent is part of our EPO server but its a major pain. normally we can just run a server side upgrade and push the new client and any updates to the end user machines but in a few cases (a lot in this case sigh) i am going to have to otherwise manually strip the agent off and then clean all registry references to it before i can then reinstall the new agent and pull the updates. too much effort hence script. – Lex – 2012-09-25T15:11:15.700

1OK, you convinced me. What exact error do you get when it tries to delete it? Is it that the key was not found? Maybe you can expand the shell variables in an echo just prior to calling the delete, in order to see what %1 is? – allquixotic – 2012-09-25T15:15:44.107

2Are you getting any errors? Also, put quotes around %1. A lot of those McAfee keys have spaces in them. – Kasius – 2012-09-25T15:21:52.320

no errors, but as allquixotic said the keys have a space in them. ive actually tried adding "" marks either side of the value but it doesnt seem to parse them at all. will try it again, thanks for the assist guys – Lex – 2012-09-25T16:08:40.833

for debugging, I would take out @echo off, so I could see what is happening line by line – SeanC – 2012-09-26T15:20:11.127

hello all, i have returned. thanks for all the advice, i ::'d my @ echo off and checked my outputs, the syntax of /f %1 rather than %1 /f was the issue as @Kasius said. it also did require the "" marks either side of the value. ran into some permission errors but solved those with an addition of a few lines using regini.exe. thanks for all the help, gonna accept the answer below. – Lex – 2012-09-27T16:59:35.000

Answers

6

It looks like reg delete needs the /f at the end:

C:\TEMP>reg delete /f hkcu\test
ERROR: Invalid key name.
Type "REG DELETE /?" for usage.

C:\TEMP>reg delete hkcu\test /f
The operation completed successfully.

Kasius

Posted 2012-09-25T15:02:19.007

Reputation: 498