In Visual Basic Script, how do you Enumerate all the subkeys of a given registry key when it is a 64 bit key?

I have some visual basic script code to recursively delete registry keys.  It works fine on the 32bit keys (under HKEY_LOCAL_MACHINE\Software\Wow6432Node) but does not execute under the 64 bit keys (at HKEY_LOCAL_MACHINE\Software).  What do I need to make to the code to get this to execute properly for both 32 and 64 bit keys?  I've worked on this for a while scouring the internet and making several approaches.  Hopefully someone out there has the solution.   

Begin Code:


Sub DeleteSubkeys(Key, strKeyPath)
 Dim arrSubkeys, strSubkey
    objRegistry.EnumKey Key, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys Key, strKeyPath & "\" & strSubkey
    End If

    objRegistry.DeleteKey Key, strKeyPath
End Sub


End of Code

BTW - I did run across this code at ITNinjas while I was looking for the 64 bit solution so the general problem has been worked on before.

0 Comments   [ + ] Show comments

Answers (2)

Posted by: SMal.tmcc 1 year ago
Red Belt

Call the 64 bit vbs exe file not the 32.  If you are using kace to call the script the client is 32 bit so it calls the 32bit vbs exe file (cscript or wscript)  those files exist as 32 and 64 bit executables.

On a x64 call the exe using the sysnative command vs standard path variables.







Posted by: lkitsmiller 1 year ago
Yellow Belt

I did find the answer.  Change the 64 to a 32 when referring to 32 bit keys.  Detailed code below:

Option Explicit
Dim oCtx, oLocator, oReg, Return, arrSubKeys, strSubKey

Const HKEY_LOCAL_MACHINE = &H80000002

DeleteSubkeys HKEY_LOCAL_MACHINE, "Software\Mykey", 64

Sub DeleteSubkeys (Key, strKeyPath, RegType)

 Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
 oCtx.Add "__ProviderArchitecture", RegType

 Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
 Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")

 oReg.EnumKey Key, strKeyPath, arrSubKeys
 If IsArray(arrSubkeys) Then
  For Each strSubkey In arrSubkeys
   DeleteSubkeys Key, strKeyPath & "\" & strSubkey, RegType
 End If
    Return = oReg.DeleteKey(Key, strKeyPath)
End Sub

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:


This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ