Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an easy (automated) way to delete all subkeys in a key in the Windows registry without deleting the key itself?


share|improve this question
up vote 1 down vote accepted

Do you know what the sub-keys are in advance? If so you can do it with a .reg file using something like this to delete all sub-keys of Test:

Windows Registry Editor Version 5.00


The minus sign at the start of the line tells it to delete that key, full syntax here:

If not, then you're looking for a script that'll enumerate all the sub-keys and then go through deleting them all one by one. I've got one that'll do this at work, but I'm at home and can't get to it!

share|improve this answer
Question is actually a moot point now, I don't need to do this anymore. And no, I do not know the subkeys in advance. Thanks anyway! – Cameron Aug 31 '09 at 23:00

With Windows7 or Vista, you can use Powershell commands like this, referring to the registry path the same way that you refer to a file system path:

Remove-Item -Path HKLM:\Software\Test\Key1 -Recurse
Remove-Item -Path HKLM:\Software\Test\Key2 -Recurse
Remove-Item -Path HKLM:\Software\Test\Key3 -Recurse
Remove-Item -Path HKLM:\Software\Test\Key4 -Recurse
share|improve this answer

Here is the powershell way to delete all the subkey of a Registry key:

$path = "Any valid Path ..."
(gci $path).PsPath  | foreach { if($_){Remove-Item $_ -Force} }

For Example :

$path = "HKLM:\Software\Policies\Microsoft\Windows\RemovableStorageDevices"
(gci $path).PsPath  | foreach { if($_){Remove-Item $_ -Force} }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.