3

I'm want to edit an already production .cmd script file, 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 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
]

NOTE: I am using Windows 2000 x86 Operating System. I'm not sure whether this will have any effect on how things work?

Derek
  • 31
  • 1
  • 1
  • 3

2 Answers2

7

What about

 Reg Delete HKLM\SOFTWARE\Microsoft\CurrentVersion\SampleKey

?

Type

 reg Delete /? 

for more info

sgmoore
  • 652
  • 5
  • 10
  • The method from @nullity will also work, but I prefer this method for simple changes as it's easily readable. Use the .reg approach when you want to make large changes to the registry. The .reg method also allows you to have one .reg file which deletes and adds keys etc – Dan Jan 19 '12 at 11:27
  • @sgmoore when I type 'reg Delete /?' in cmd I'm getting an error stating that 'reg' is not recognised as an internal or external command.... – Derek Jan 19 '12 at 11:36
  • What/where are you executing this? What about simply typing `reg /?`? REG should be available anywhere command prompt is! I agree that the REG command is more readable, and I've edited my answer to include it as the preferred method. I was fixated on the regedit import from the posters suggestion! – Josh Atkins Jan 19 '12 at 11:45
  • @nullity thanks for the edit... Strange - I have tried the reg command from my 2008 server machine and its fine. However from my Windows 2000 machine it does not work??? is there something I should be enabling here? – Derek Jan 19 '12 at 11:52
  • Eek, Windows 2000. I don't have a 2K install around to test it on, it is possible that REG doesn't exist that far back though I was under the impression anything NT or higher based should. I've only tested it as low as Windows XP. – Josh Atkins Jan 19 '12 at 11:59
  • 1
    @nullity found out the problem (with your assistance of course!! :)) reg.exe is not installed by default on Win2k. However you can install it manually from the CD via Support/Tools/Setup.exe. All working now :) Thanks. – Derek Jan 19 '12 at 12:04
  • Awesome! That's a strange one but one to remember, thanks for reporting back. :) Do keep in mind the existence of it on other boxes if this is being replicated or called elsewhere (a nice comment in the script if you like your predecessors, too). – Josh Atkins Jan 19 '12 at 12:43
2

You need to execute regedit.exe (with a /s switch for silent) instead of simply cmding the .reg.

regedit /s "\\networkdrive\regfiles\deleteSampleKey.reg"

should do what you want (untested).

EDIT: As per @sgmoore 's answer the preferred method should be using REG, unless you have a large number of entries to clean up.

reg /?

Josh Atkins
  • 783
  • 4
  • 6