Solved

Delete Registry Key and Sub Keys with VBScript.

Posted on 2006-11-12
9
3,196 Views
Last Modified: 2008-01-09

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
Comment
Question by:Matthew_Way
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17926664
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
 

Author Comment

by:Matthew_Way
ID: 17926929

I get "Error deleting key: 2"
0
 
LVL 12

Expert Comment

by:jkaios
ID: 17927532
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
 
LVL 12

Expert Comment

by:jkaios
ID: 17927546
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:Sethi
ID: 17927712
0
 
LVL 29

Accepted Solution

by:
Nightman earned 250 total points
ID: 17928101
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
 

Author Comment

by:Matthew_Way
ID: 17935064
Hi Nightman,

The new constants did the trick.

Where do I get a list of the error numbers ?

Thanks
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17936174
Trial and error ;)

Those are just what I have noted.
0
 

Author Comment

by:Matthew_Way
ID: 17936273
Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now