Delete registry key or value via a CMD script?

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
]

I would recommend using the REG command, rather than creating and importing .reg files.

reg delete "HKCU\Some\Registry\Path" /f

or

reg delete "HKLM\Some\Registry\Path" /f

These 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"=-

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