Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help Deleting Registry Keys with subkeys and values

1 view
Skip to first unread message

Dan Phillips

unread,
Mar 6, 2003, 9:36:09 AM3/6/03
to
Here is the command I am trying to run:


Set objShell = CreateObject("WScript.Shell")
objShell.RegDelete "HKLM\SOFTWARE\Microsoft\Active
Setup\Installed Components\"
Set objShell = Nothing


This is not working. It works when deleting a value or
subkey but not the entire key. Is there a way of deleting
an entire key with subkeys and values without enumurating
the key and deleting each subkey?

Thanks,
Dan

Torgeir Bakken (MVP)

unread,
Mar 6, 2003, 2:49:40 PM3/6/03
to
Dan Phillips wrote:

> Here is the command I am trying to run:
>
> Set objShell = CreateObject("WScript.Shell")
> objShell.RegDelete "HKLM\SOFTWARE\Microsoft\Active
> Setup\Installed Components\"
>

> This is not working. It works when deleting a value or
> subkey but not the entire key. Is there a way of deleting
> an entire key with subkeys and values without enumurating
> the key and deleting each subkey?

Hi

You can delete keys (with subkeys) and values with a registry file

The following registry file will delete the key HKEY_LOCAL_MACHINE\SOFTWARE\Foo

REGEDIT4

[-HKEY_LOCAL_MACHINE\SOFTWARE\Foo]


(note the - sign)

You can use FSO to create this registry file on the fly, and then import it
silently with regedit /s, like this:


Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

sTempRegFile = oFSO.GetSpecialFolder(2) & "\DeleteRegKey.reg"

Set fRegFile = oFSO.CreateTextFile(sTempRegFile, True)

fRegFile.WriteLine "REGEDIT4"
fRegFile.WriteLine
fRegFile.WriteLine "[-HKEY_LOCAL_MACHINE\SOFTWARE\Foo]"
fRegFile.WriteLine ' always end registry files with a blank line.
fRegFile.Close

oShell.Run "regedit.exe /s " & sTempRegFile, 0, True
oFSO.DeleteFile sTempRegFile, True

You can use a VBScript/WMI recursive sub as well:


strComputer = "."

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

Set oReg=GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")

DeleteRegistryKey HKLM, "SOFTWARE\SomeKey"


Sub DeleteRegistryKey(hive,key)
On Error Resume Next
rc = oReg.EnumKey(hive,key,arSubKeys)
For Each sKey In arSubKeys
If Err Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey hive, key & "\" & sKey
Next
rc = oRegistry.DeleteKey(hive,key)
End Sub


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


Fosco

unread,
Mar 8, 2003, 1:11:06 AM3/8/03
to
"Dan Phillips"

For win9x

DOKEY *.vbs

set oShell = CreateObject("WScript.Shell")
oShell.run"regedit.exe /s C:\Dokey.reg"

FileDOKEY *.reg

REGEDIT4

[HKEY_CURRENT_USER\Software\Test]

[HKEY_CURRENT_USER\Software\Test\First]
"Secret"="11111"

[HKEY_CURRENT_USER\Software\Test\First\Second]


DELETEKEY *.vbs

set oShell = CreateObject("WScript.Shell")
oShell.run"regedit.exe /s C:\Delete.reg"

File DELETE *.reg

REGEDIT4

[-HKEY_CURRENT_USER\Software\Test]

--
Fosco

Torgeir Bakken (MVP)

unread,
Mar 10, 2003, 1:20:07 PM3/10/03
to
"Torgeir Bakken (MVP)" wrote:

> You can use a VBScript/WMI recursive sub as well:
>
> strComputer = "."
>
> Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
>
> Set oReg=GetObject _
> ("winmgmts:{impersonationLevel=impersonate}!\\" _
> & strComputer & "\root\default:StdRegProv")
>
> DeleteRegistryKey HKLM, "SOFTWARE\SomeKey"
>
> Sub DeleteRegistryKey(hive,key)
> On Error Resume Next
> rc = oReg.EnumKey(hive,key,arSubKeys)
> For Each sKey In arSubKeys
> If Err Then
> Err.Clear
> Exit Sub
> End If
> DeleteRegistryKey hive, key & "\" & sKey
> Next
> rc = oRegistry.DeleteKey(hive,key)
> End Sub

There is one error in the script above, the line
rc = oRegistry.DeleteKey(hive,key)
needs to be changed to
rc = oReg.DeleteKey(hive,key)

0 new messages