Solved

Delete Registry Key and Sub Keys with VBScript.

Posted on 2006-11-12
9
3,200 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 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

10 Experts available now in Live!

Get 1:1 Help Now