Solved

Delete Registry Key and Sub Keys with VBScript.

Posted on 2006-11-12
9
3,203 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

789 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