• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3228
  • Last Modified:

Delete Registry Key and Sub Keys with VBScript.


I want to delete a branch from the registry key including sub keys.

I found the following VB example from a previous post ( Q_21245922 ).

Can some one show me how to do the same thing in VBScript.


Option Explicit

Public Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function SHDeleteKey Lib "shlwapi.dll" Alias "SHDeleteKeyA" (ByVal hkey As Long, ByVal pszSubKey As String) As Long

Private Sub Command1_Click()
SHDeleteKey HKEY_CURRENT_USER, "Software\Microsoft\ABC"
End Sub
0
Matthew_Way
Asked:
Matthew_Way
  • 3
  • 3
  • 2
  • +1
1 Solution
 
NightmanCTOCommented:
Try this:

' This code deletes a registry key.
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
const HKCU = &H80000001 'use &H80000001 for HKEY LOCAL MACHINE
strKeyPath = "Software\Microsoft\ABC"      ' e.g. Software\Microsoft\ABC
strComputer = "." ' e.g. server01 (use "." for local server or servername for remote server)
' ------ END CONFIGURATION ---------
set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
intRC = objReg.DeleteKey(HKCU, strKeyPath)
if intRC <> 0 then
   WScript.Echo "Error deleting key: " & intRC
else
   WScript.Echo "Successfully deleted key " & strKeyPath
end if

0
 
Matthew_WayAuthor Commented:

I get "Error deleting key: 2"
0
 
jkaiosCommented:
Const HKEY_CURRENT_USER = &H80000001
Const HK_CU = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HK_LM = &H80000002


Private Sub Command1_Click()
  If DeleteRegKey(HKEY_CURRENT_USER, "Software\Microsoft\ABC") Then
     MsgBox "Key was deleted successfully."
  End If
End Sub


'============================================
Public Function DeleteRegKey(RegHive, sKeyPath) As Boolean

 On Error GoTo Err_Handler
 
 Dim oReg
 Set oReg = CreateObject("Wscript.Shell")
 
 Select Case RegHive
   Case HKEY_CURRENT_USER, HK_CU
      oReg.RegDelete "HKCU\Software\" & sKeyPath
   Case HKEY_LOCAL_MACHINE, HK_LM
      oReg.RegDelete "HKLM\Software\" & sKeyPath
   Case Else
      Exit Function
 End Select
 
 ' If no error occurs, then the key was successfully deleted.
 DeleteRegKey = True
 
 Exit Function
Err_Handler:
 DeleteRegKey = False

End Function
'============================================
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkaiosCommented:
The following code creates a key and two values, reads them, and deletes them.

'============================================================
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\", 1, "REG_BINARY"
WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\MindReader", "Goocher!", "REG_SZ"

bKey = WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\")
WScript.Echo WshShell.RegRead("HKCU\Software\ACME\FortuneTeller\MindReader")

WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\MindReader"
WshShell.RegDelete "HKCU\Software\ACME\FortuneTeller\"
WshShell.RegDelete "HKCU\Software\ACME\"
'============================================================
0
 
NightmanCTOCommented:
Sorry
const HKCU = &H80000001 'HKEY_CURRENT_USER
const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

That particular script will do the job, but will raise an error 2 is the key (the strKeyPath variable) doesn't exist.
Error 5 will be raised if there are sub keys (they won't automatically be deleted).
0
 
Matthew_WayAuthor Commented:
Hi Nightman,

The new constants did the trick.

Where do I get a list of the error numbers ?

Thanks
0
 
NightmanCTOCommented:
Trial and error ;)

Those are just what I have noted.
0
 
Matthew_WayAuthor Commented:
Thanks.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now