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

Re: Delete Registry sub-key by Torgeir

Torgeir
Mon Dec 22 13:02:18 CST 2003

Mark wrote:

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

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



Re: Delete Registry sub-key by Mark

Mark
Mon Dec 22 13:43:42 CST 2003

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.Bakken-spam@hydro.com> wrote in message
news:3FE73FBA.3F1A53B3@hydro.com...
> Mark wrote:
>
> > 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.
>
> 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
>
>



Re: Delete Registry sub-key by Torgeir

Torgeir
Mon Dec 22 15:16:22 CST 2003

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


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



Re: Delete Registry sub-key by Mark

Mark
Tue Dec 23 14:58:36 CST 2003

Thanks Torgeir, that works great!!

Mark
"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:3FE75F26.D93A78F2@hydro.com...
> 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".
>
>
> --
> 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
>
>