Delete all subkeys in a registry key

Solution 1:

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

Solution 2:

Excerpted from Working with Registry Keys:

If you wanted to remove all items within HKCU:\CurrentVersion but not HKCU:\CurrentVersion itself, you could instead use:

#Requires -Version 3.0
Remove-Item -Path HKCU:\CurrentVersion\* -Recurse

Note: Registry values belonging to HKCU:\CurrentVersion are not removed.


Solution 3:

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

[-HKEY_LOCAL_MACHINE\Software\Test\Key1]
[-HKEY_LOCAL_MACHINE\Software\Test\Key2]
[-HKEY_LOCAL_MACHINE\Software\Test\Key3]
[-HKEY_LOCAL_MACHINE\Software\Test\Key4]

The minus sign at the start of the line tells it to delete that key, full syntax here: http://support.microsoft.com/kb/310516

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!