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

Delete Registry sub-key

5 views
Skip to first unread message

Mark

unread,
Dec 22, 2003, 1:42:07 PM12/22/03
to
In vbscript, I want to be able to delete multiple registry entries with
sub-keys. I have only been able to delete a key if there are no sub-keys.
If there are sub-keys I get an error when I try deleting the key. I figured
that I would just enumerate the sub-keys but I didn't have any luck with
this. How would I do this if there are 4 or 5 levels underneath the original
entry that I want to remove, and some may only have 0 to 1.

Thanks.
Mark

Torgeir Bakken (MVP)

unread,
Dec 22, 2003, 2:02:18 PM12/22/03
to
Mark wrote:

Hi

To delete a key that have subkeys, you can use VBScript\WMI and a recursive sub
(a sub that calls itself) to delete the key(s), see sub DeleteRegistryKey in
the link below:

http://groups.google.com/groups?selm=3FCCD280.8B890181%40hydro.com


You can also delete a registry key with any sub-keys with a registry file:

http://groups.google.com/groups?selm=3FCE6DF8.830634EA%40hydro.com

--
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


Mark

unread,
Dec 22, 2003, 2:43:42 PM12/22/03
to
I copied that script and when I run it, it does not delete the registry key.
I remarked out the "On Error Resume Next" line and ran it again and I get an
error on the "For Each sSubKey in aSubKeys" line saying that the "Object not
a collection". Any ideas what I need to do? I did not try the reg file
because I want to stick with vbscript, the script that I am using does other
things in addition to modifying the registry.

Mark
"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:3FE73FBA...@hydro.com...

Torgeir Bakken (MVP)

unread,
Dec 22, 2003, 4:16:22 PM12/22/03
to
Mark wrote:

> I copied that script and when I run it, it does not delete the registry key.
> I remarked out the "On Error Resume Next" line and ran it again and I get an
> error on the "For Each sSubKey in aSubKeys" line saying that the "Object not
> a collection". Any ideas what I need to do?

Oops, you got the version that works on Win2k SP2 and lower only...


This one will work on all Win2k and WinXP computers, regardless of SP level
(added an IsArray(aSubKeys) test):

Const HKLM = &H80000002
sKey = "System\CurrentControlSet\Services\Myservice"

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

DeleteRegistryKey HKLM, sKey


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub

> I did not try the reg file
> because I want to stick with vbscript, the script that I am using does other
> things in addition to modifying the registry.

You can use this registry file from a vbscript file as well, using the
FileSystemObject to create the registry file on the fly and then shell out
(using the Run method) and run regedit.exe /s "some reg file".

Mark

unread,
Dec 23, 2003, 3:58:36 PM12/23/03
to
Thanks Torgeir, that works great!!

Mark
"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message

news:3FE75F26...@hydro.com...

0 new messages