How do I edit an already-in-production .cmd script file, in order to have the script delete a certain registry key in the Windows registry?
Firstly, is this even possible, and secondly (if that's not possible), could I create a .reg file and execute that file from with the .cmd file?
From within the .cmd script, it is not working:
del "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\CurrentVersion\SampleKey]"This method hasn't worked for me either:
cmd "\\networkdrive\regfiles\deleteSampleKey.reg"Then from within the .reg file:
Windows Registry Editor Version 5.00
[
-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
] 1 5 Answers
I would recommend using the REG command, rather than creating and importing .reg files.
reg delete "HKCU\Some\Registry\Path" /for
reg delete "HKLM\Some\Registry\Path" /fThese commands can be entered directly into the batch (.cmd) file.
As described here and here by Microsoft under "Removing registry entries" create a .reg file containing the keys or values you want to remove.
Delete Key
You can remove a registry key by placing a hyphen (minus character) "-" in front of a key like that:
[-HKEY_LOCAL_MACHINE\SOFTWARE\YourSoft\MyKey]Delete Value
And to remove a registry value, place a hyphen (minus character) "-" after the = character like that:
[HKEY_LOCAL_MACHINE\SOFTWARE\YourSoft\MyKey]
"MyEntry"=- 5 I would avoid using another script as you can do this in a .cmd file using the REG commands.
You can do something similar to this:
REG DELETE "HKEY_CURRENT_USER\SOFTWARE\SomeProgram"If you would like to delete only specific entries then you should add a /v "EntryName" argument after the path to the key. E. g:
REG DELETE "HKEY_CURRENT_USER\SOFTWARE\SomeProgram" /v "EntryName"Both of these will cause a warning to be issued before deleting the values. To avoid that, you should use the /f argument at the end.
REG DELETE "HKEY_CURRENT_USER\SOFTWARE\SomeProgram" /f 1 I use the following method. This is part of a MRU ripper for Windows XP, that might help. This is the text of a batch file, that is run from CMD or from a shortcut. It creates a registry file using simple echo redirections, then imports it.
REM *** START REGISTRY SHREDDER ***
ECHO CREATING MRU REGISTRY
ECHO . ECHO > "%TMP%\MRUKILL.reg" Windows Registry Editor Version 5.00
ECHO >> "%TMP%\MRUKILL.reg" [-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Doc Find Spec MRU]
ECHO >> "%TMP%\MRUKILL.reg" [-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FindComputerMRU]
REM *** IMPORT THAT REG TO WIPE OUT THEM KEYS AND VALUES ****
regedit "%TMP%\MRUKILL.reg"I am not a coder, I just do some batch stuff for myself, so I often can't even read what I wrote, but I would use that as a template for doing something else.
The echos are redirected (>) to a file, with the single > for the first redirection, and >> to append to the same file. The - sign is used to remove a registry entry. It would probably be good to leave off the last line, and view the file it created before importing. Importing can be done silently, but I won't do that myself.
Disclaimer: Because a person can do some serious damage to the system removing registry entries, an disk image backup that can return the system when the system fails is necessary.
Proper permissions, elevations, and some entries in the registry will not just allow the user to destroy them, without setting the permissions, because of their importance.
Here is A "ask the user" part, added just for fun.
ECHO REM *** ASK the USER FIRST ***
ECHO DO YOU WANT TO DELETE REGISTRY MRUS?
SET /P Choice2=type "Y" to Mangle your registry, - -
ECHO .
IF /I "%Choice2%"=="Y" GOTO SHREDD
ECHO OK WELL THEN I WON'T
ECHO By By REM *** USER DECIDED AGAINST IT ***
ECHO .
PAUSE
GOTO FINISH 3 I'm using Windows 7 and this is what I got through CMD:
Fri 08/08/2014 8:13:51.72 | C:\Users\MrCMD
>reg.exe delete /?
REG DELETE KeyName [/v ValueName | /ve | /va] [/f] KeyName [\\Machine\]FullKey Machine Name of remote machine - omitting defaults to the current machine. Only HKLM and HKU are available on remote machines. FullKey ROOTKEY\SubKey ROOTKEY [ HKLM | HKCU | HKCR | HKU | HKCC ] SubKey The full name of a registry key under the selected ROOTKEY. ValueName The value name, under the selected Key, to delete. When omitted, all subkeys and values under the Key are deleted. /ve delete the value of empty value name (Default). /va delete all values under this key. /f Forces the deletion without prompt.
Examples: REG DELETE HKLM\Software\MyCo\MyApp\Timeout Deletes the registry key Timeout and its all subkeys and values REG DELETE \\ZODIAC\HKLM\Software\MyCo /v MTU Deletes the registry value MTU under MyCo on ZODIACOr, I think we can DELETE some keys or MODIFY some values by this algorithm:
- Export registry location of keys/values we want to delete/modify into a file (File01.reg).
- Edit/modify appropriate key/value and save to new file (File02.reg).
- Import that modified file (File02.reg) into Windows Registry.
Reference to EXPORT registry.
Fri 08/08/2014 8:24:53.19 | C:\Users\mardir01
>reg.exe export /?
REG EXPORT KeyName FileName [/y] Keyname ROOTKEY[\SubKey] (local machine only). ROOTKEY [ HKLM | HKCU | HKCR | HKU | HKCC ] SubKey The full name of a registry key under the selected ROOTKEY. FileName The name of the disk file to export. /y Force overwriting the existing file without prompt.
Examples: REG EXPORT HKLM\Software\MyCo\MyApp File01.reg Exports all subkeys and values of the key MyApp to the file File01.regReference to IMPORT registry.
>reg.exe import /?
REG IMPORT FileName FileName The name of the disk file to import (local machine only).
Examples: REG IMPORT File02.reg Imports registry entries from the file File02.regBrighter ideas for improvement are welcome. :) :) :)