Link to home
Start Free TrialLog in
Avatar of szes3
szes3

asked on

Deleting a Registry key via script (getting error Unable to remove registry key. Error code 8007005. )

We are having problems deleting a registry key from Windows Xp sp2 machines. We need to create a script that I can roll out to our end users that removes the following registry key.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\RightFax Fax Printer

I have created a VBS script that should remove the registry entry. But I get an error Unable to remove registry key. Error code 8007005.

Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell. RegDelete"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\RightFax Fax Printer\"


And have local administrator rights to the machine. It doesnt have to be a vb script. If a bat file could do the same thing that would be great also.

Thanks in advance for any insight you may provide.
Avatar of Zaheer Iqbal
Zaheer Iqbal
Flag of United Kingdom of Great Britain and Northern Ireland image

You may need to take ownership of the key first.
Avatar of sundaramkumar
sundaramkumar

Uses WMI to delete the registry key HKLM\SOFTWARE\System Admin Scripting Guide.

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\System Admin Scripting Guide"
 
oReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath

for more info and scripts see this url
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/registry/#DelRegKey.htm
strKeyPath = "yourkey with the path here"
Avatar of szes3

ASKER

Sundaramkumar,

So it should look like this.(see below)  And save it as a .vbs and run it?  If so its a no go. Never used  WMI before so I may be doing something wrong.

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\RightFax Fax Printer"
oReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath


Avatar of szes3

ASKER


Sundaramkumar,
Here is the code I am using.

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\RightFax Fax Printer\"
 
oReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
Avatar of szes3

ASKER

1stITMAN,

I have full permission on the key. All users are local administrators to the machine they use. Just to be on the safe side I went in and checked it. I have full control over that key.

When you mean take ownership to the key is there something else I have to do?
ASKER CERTIFIED SOLUTION
Avatar of lancebm
lancebm
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've always been more familiar with batch files than any other type of scripts.  I use regedit to add/remove keys on my workstations on my network.

Create a registry information file demo.reg
It is a text file, so any text editor will work.  It only needs 3 lines (if you count the blank line which I have always used)

REGEDIT4

"[-HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\RightFax Fax Printer\]"

Then the workstations need to run
regedit /s \\domaincontroller\netlogin\demo.reg

That line can be added to their existing login script or you can create a new one.

More info for running regedit from a command line can be found here...

http://support.microsoft.com/kb/310516
search for "SetACL.exe" on sourceforge.net  it allows this sort of thing from a batch file.
Avatar of szes3

ASKER

This worked like a champ.

const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\RightFax Fax Printer"

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

DeleteSubkey strKeypath
wscript.quit

Sub DeleteSubkey(strKeyPath)
   
    'Get a string array of subkeys
    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

    If IsArray(arrSubkeys) Then
      'Iterate through each SubKey
            For Each strSubkey In arrSubkeys
                        'Delete any Subkeys that are in the current subkey
                        DeleteSubkey strKeyPath & "\" & strSubkey
            Next
    End If
 
    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath

End Sub

Thanks again for the fast repsonse and all the friendly help