Solved

Delete Registry Key and Sub Keys with VBScript.

Posted on 2006-11-12
9
3,216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

728 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