Previous Page
Next Page

Deleting Registry Information

If you need to delete a registry key, perhaps as a result of cleaning up after a virus, uninstalling software, or cleaning up after you're finished with the keys you created, you can use the DeleteKey method of StdRegProv. The next script, DeleteRegKey.vbs, illustrates how easy this is to do. Additional cautions about having a good backup and testing on other machines are applicable here! Be careful!

Though much of the script is similar to other registry provider scripts, a couple of items are important to note here. Notice in the Worker information section of the script that you have to delete the subkey before you can delete the parent key. The DeleteKey method deletes only keys. If you have a large section of the registry you need to lobotomize, you could use the EnumKey method and, as you iterate through the array, you could use DeleteKey.

DeleteRegKey.vbs

Option Explicit
On Error Resume Next
Dim strKeyPath 'the portion of registry to read
Dim strComputer 'the target computer
Dim objReg 'holds connection to registry provider
Dim subKey 'used to enumerate throught the array
Dim arrSubKeys 'holds the subkeys
Dim ParentKey
Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKU = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG

ParentKey = "SOFTWARE\EdWilson"
strKeyPath = "SOFTWARE\EdWilson\VbscriptBook"

strComputer = "."

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


objReg.DeleteKey HKLM, strKeyPath
objReg.DeleteKey HKLM, ParentKey

If Err.Number = 0 Then
  WScript.Echo("Deleted key:" & strKeyPath)
  WScript.Echo("Deleted subKey: " & ParentKey)
Else
  WScript.Echo("Error number " & Err.Number & "occurred")
End If


Previous Page
Next Page