Delete multiple registry keys with one VBScript even if subkeys have values

Hello,

We recently removed Novell from our environment.  We had run a login script to automatically remove the Novell client upon login.  Once we felt confident that all clients had been removed, we turned off the Novell server.  We started having issues where some users were experiencing a tremendous slowdown.  In researching we found that some computers were still looking for the Novell server for various items still reflected in the registry.  We have located all these items and I would now like to develop a VBScript to delete all these keys.

The PCs are all Windows XP and apparently there is a limitation with VBScript removing a key if there are subkeys that contain data.

Does anyone have a simple way I can place this in a VBScript where I list all the keys in an array (or something) that will search all subkeys and delete whatever is needed to allow the base key to be removed?

Thanks fo any help you can offer..... Tamara
LVL 1
TamaraSJAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TamaraSJAuthor Commented:
I wanted to note that I found this script that I believe will work; however, I'm wondering what I need to do to incorporate multiple keys that need to be removed.  I know the strKeyPath is how I delete one, but do I then have to copy and paste this script multiple times using different variables for each key?  Or is there a simple way to have the sub routine call each defined key variable?

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
strKeyPath = "Software\Test"

Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")

DeleteSubkeys HKEY_CURRENT_USER, strKeypath

Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
    objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys

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

    objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
End Sub
0
sr75Commented:
Here is a site that has directions to scripting deletion of reg keys:

http://activexperts.com/activmonitor/windowsmanagement/adminscripts/registry/
0
sr75Commented:
You do not have to copy the key, but create an array and loop through a "For i=0 to ubound(array)".  then you will only have to write the deletion code once.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

TamaraSJAuthor Commented:
Thank you sr75; however, I don't see anything there for deleting a registry key that has subkeys.  With Windows XP, it seems to error on key deletion if there are any subkeys that contain values.  I found the script I listed above and I see how it would work to search out all subkeys and delete each before returning to delete the main key.  The problem is, I'm uncertain how to incorporate multiple key sections.

Example, I need to delete all of the following and everything within, but they all contain subkeys:

HKEY_CURRENT_USER\Software\Netware\Nal
HKEY_LOCAL_MACHINE\SOFTWARE\NetWare\NAL
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Novell
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print\Novell

and about 23 others.

I would like to know the best way to incorporate all of these above keys in the script I attached to have them all included.

Again, any help provided will be greatly appreciated.  Thanks.... Tamara
0
TamaraSJAuthor Commented:
sr75,

Is there anyway you can incorporate that into the script I provided as an example with the keys I listed above?  I'm just not exactly sure how to include this.

Tamara
0
sr75Commented:
Here is what it would look like, but this script will not run as it is because of you need the constants for each registry key.  But that is pretty much similar to how I would delete multiple registry keys.
Const HKEY_CURRENT_USER = &H80000001
 
strComputer = "."
strKeyPath = "Software\Test"
 
arrKey = array("HKEY_CURRENT_USER\Software\Netware\Nal",_
        "HKEY_LOCAL_MACHINE\SOFTWARE\NetWare\NAL",_
        "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Novell",_
        "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print\Novell"
 
For i=0 to ubound(arrKeys)
 
Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")
 
strKeypath = arrKeys(i) & strKeypath
 
DeleteSubkeys HKEY_CURRENT_USER, strKeypath
 
Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
    objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
 
    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
        Next
    End If
 
    objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
End Sub
 
Next

Open in new window

0
TamaraSJAuthor Commented:
I'm not sure exactly what you mean by the "constants".

I'm assuming I would also replace the strKeyPath variable and change those references in the script to arrKey based on this example, correct?

Would I also change DeleteSubkeys HKEY_CURRENT_USER, strKeypath to just simply DeleteSubKeys arrkey or should I create a variable for each path HKCU, HKLM, etc? and use those in place of the full path info.

I'm really sorry, this script is making me feel like an idiot.
0
sr75Commented:
You should create a constant for each Hive that you use.  You already did for one hive:

Const HKEY_CURRENT_USER = &H80000001

Except for the Hive key the rest of the path should probably be in the strings of the array.

I could be much more helpful if I had my script library with me.  I usually pull from them to write my scripts because they would usually be functioning to show how it works.  

I cannot write the script for you, but I can show you something similar to what I would:
Const HKEY_CLASSES_ROOT 	= &H80000000
Const HKEY_CURRENT_USER 	= &H80000001
Const HKEY_LOCAL_MACHINE 	= &H80000002
Const HKEY_USERS 		= &H80000003
Const HKEY_CURRENT_CONFIG 	= &H80000005
 
 
arrKeys = array(HKEY_CURRENT_USER & "key path",_
                HKEY_CURRENT_USER & "key path",_
                HKEY_CURRENT_USER & "key path",_
                HKEY_LOCAL_MACHINE & "key path")
 
Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")
 
For i=0 to ubound(arrKeys)    'this loops through the arrKeys array"
 
          ### This is the code that needs to be figured out now ###
            
Next

Open in new window

0
TamaraSJAuthor Commented:
I'm so sorry, after I wrote that last one I realized what you meant by the "Constants".  For some reason I was thinking outside the script and thought you were looking to have me append something to the end of each registry key.

My brain is just getting fried on this one.  Thanks again, I wll see what I can figure out from here.

Tamara
0
RobSampsonCommented:
Hi Tamara,

VBScript isn't very good at deleting SubKeys, whereas the DOS Reg Delete command does not care about them!  This should work:

'===============
Const regRoot = "HKCU"

strComputer = "."
strKeyPath = "Software\Test"

Set objShell = CreateObject("WScript.Shell")
strCommand = "cmd /k REG DELETE /F """ & HKCU & "\" & strKeyPath & """"
objShell.Run strCommand, 1, True
Set objShell = Nothing
'===============

When you've finished testing, change the cmd /k to cmd /c, and change
objShell.Run strCommand, 1, True
to
objShell.Run strCommand, 0, True

to make it all hidden.

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TamaraSJAuthor Commented:
Rob,

Thanks.... While setting this up was kind of a pain with 27 keys to delete, ultimately it worked and saved me a great deal of time (after I wasted a ton of time trying to get it to work the other way).

Here is a sample of what I opted to do.......

Set WshShell = CreateObject("WScript.Shell")

sCmd = "reg delete HKCR\*\shellex\ContextMenuHandlers /f"
nRC = WshShell.Run ( sCmd, 4, True )

These two lines were repeated 27 times in my script and all appears to work well.

Thanks again..... Tamara
0
RobSampsonCommented:
Wow!  Yeah, if you did have 27 different keys to delete, you'd have to write them all out in some form or other.  Personally, I might use an Array for that, eg:

arrKeys = Array( _
   "hkcu\key\key", _
   "hkcr\key\key", _
   "hkcr\key2\key2" _
)

For Each strKey In arrKeys
   sCmd = "reg delete /f """ & strKey & """"
   nRC = WshShell.Run ( sCmd, 4, True )
Next


But anyhow you get the job done, at least it's done ;-P

Regards,

Rob.
Next
0
TamaraSJAuthor Commented:
I was desperately trying to use an array (with the vbscript method) with no luck.  I spent many hours on this and after seing your e-mail, this just worked out better and saved me a great deal of time.  Again..... Thank you.

Tamara
0
RobSampsonCommented:
No worries....if you get stuck, feel free to ask....there's many experts here that can point you in the right direction....even if it takes some time for us to respond....

Also, just a tip, don't forget about outputting a command you're trying so you can see visually whether the command would work or not, and if the syntax is correct.

For example, with the sCmd above, you can put this:
   sCmd = "reg delete /f """ & strKey & """"
   sCmd = InputBox("About to execute:", "Command", sCmd)
   nRC = WshShell.Run ( sCmd, 4, True )

and then you get the command in a text box, which you can copy and paste to a command prompt to test yourself.

Regards,

Rob.
0
rickc11Commented:
I had reviewed this post and found good information.  My problem maybe simple but here goes.
I can manually look for and remove all entries that have "ActivCard Gold" throughout the registry.  How do I apply the value ActivCard Gold which is what I'm looking for in the above script?  I'm a little weak in vbscripting.

Thank you
Rick
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.