I am using VB script to remove reg. key. In my vb, I am running .reg file to delete it, I have also tried using the following code to remove but not luck:

const HKEY_CURRENT_USER = &H80000002
strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\[ProductName]"

oReg.DeleteKey HKEY_CURRENT_USER, strKeyPath

Can someone think or see something here that I might be missing?

Answer Summary:
0 Comments   [ + ] Show Comments


Please log in to comment


I use REG DELETE "HKEY_LOCAL_MACHINE... in a batch file to remove registry keys and i also use this vbs as well.

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002

Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
Set wshNetwork = CreateObject("WScript.Network")
Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
Set objRegistry = wmiNameSpace.Get("StdRegProv")

' Deletes Key with alle subkeys
sPath1 = "Software\..."
sPath2 = "Software\..."
sPath3 = "SOFTWARE\....\"

lRC = DeleteRegEntry(HKEY_CURRENT_USER, sPath1)
lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath2)
lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath3)

Function DeleteRegEntry(sHive, sEnumPath)
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
If (lRC <> 0) Then
On Error Resume Next
lRC = objRegistry.EnumKey(HKEY_CURRENT_USER, sEnumPath, sNames)
For Each sKeyName In sNames
If Err.Number <> 0 Then Exit For
lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
On Error Goto 0
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
End If
End Function
Answered 03/27/2012 by: shigbee
Second Degree Black Belt

Please log in to comment
I hope you are trying to delete it from HKLM hive.. as mentioned in script const HKEY_CURRENT_USER = &H80000002

Also , Are there any other registry entries inside this hive. SOFTWARE\[ProductName]

Then you have to delete them .

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
strKeyPath = "Installer\Products"

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath

Sub DeleteSubkeys(HKEY_CLASSES_ROOT, strKeyPath)
objRegistry.EnumKey HKEY_CLASSES_ROOT, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_CLASSES_ROOT, strKeyPath & "\" & strSubkey
End If
objRegistry.DeleteKey HKEY_CLASSES_ROOT, strKeyPath
End Sub

Answered 03/27/2012 by: rock_star
Black Belt

Please log in to comment
What tool are you using to deploy the script? If your deleting from HKCU is the script running as the user?
Answered 03/27/2012 by: dchristian
Red Belt

Please log in to comment
Folks, thanks for all of your feedback. This issue has been resolved now. I caught my mistake, and now the script is working flawlessly. Once again thanks for all the input.

Answered 03/27/2012 by: 786_ak
Second Degree Green Belt

Please log in to comment
Answer this question or Comment on this question for clarity