Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Force delete Reg Key

622 views
Skip to first unread message

Phyxious

unread,
Dec 12, 2006, 12:27:00 PM12/12/06
to
I am wondering is there a way to force delete a reg key, that can't be
deleted normally through a script?

Basically I have a script that will delete the specified keys that I want,
but if a user is logged on then the keys produced by that user will not
delete. But if I manually goto the key through regedit and right click and
delete it will delete even if the user is logged on. I would like this
functionality in my script without having to log the user off. Once the
script is done running then a reboot script will run so the user will be
logged off anyways, I am just trying to save alittle time and dont want to
reboot twice.

Les

unread,
Dec 12, 2006, 1:41:39 PM12/12/06
to
How are you trying to delete the regkey via script? Is it raw VBScript or
are you chaining a .reg or .inf file to the end of your VBScript?


"Phyxious" <Phyx...@discussions.microsoft.com> wrote in message
news:801ED8D9-76C2-4177...@microsoft.com...

Phyxious

unread,
Dec 12, 2006, 1:51:00 PM12/12/06
to
Below is the following script that I am running.

Const HKLM = &H80000002
strComputer = "."

Set objReg = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\Print\Printers\"
objReg.EnumKey HKLM, strKeyPath, arrSubKeys

For Each SubKey In arrSubKeys
If InStr(SubKey, "Client") OR InStr(SubKey, "session") > 0 Then
objReg.DeleteKey HKLM, strKeyPath & SubKey
End If
Next

Set objReg = Nothing

Baard Schøyen

unread,
Dec 12, 2006, 1:54:00 PM12/12/06
to
Hi

Did you try to delete using WSHShell.RegDelete?

If not, try this:

--

Set WSHShell = Wscript.CreateObject("Wscript.Shell")
On Error Resume Next
WSHShell.RegDelete("HKCU\Software\Microsoft\KeyToDelete")
On Error Goto 0

--

Regards,
Baard Schøyen

Phyxious

unread,
Dec 12, 2006, 2:26:01 PM12/12/06
to
Tried it but I get an error when trying to delete the key that is locked by a
logged on user.

Baard Schøyen

unread,
Dec 12, 2006, 3:37:01 PM12/12/06
to
Maybe this will work:
--
Set oShell = Wscript.CreateObject("Wscript.Shell")
oShell.Run "%comspec% /c reg delete HKCU\Software\KeyToDelete /f", 0, True

Phyxious

unread,
Dec 12, 2006, 4:50:00 PM12/12/06
to
That didnt delete the keys, I replace the oShell.Run line with the line in my
for loop so im not sure if thats why it isnt deleting, but I think not. I
will try and look at it when I have more time. Thank you for your help

Baard Schøyen

unread,
Dec 12, 2006, 6:33:01 PM12/12/06
to

Hmm..
If this is a permissions/ownership problem, then maybe you can use
SubInAcl.exe. The following script let the current user take ownership on all
keys and values, removes all permissions, and assign FC permission on all
keys and values to the current user. Then it deletes the keys and values.
Worth a try!

--

' SubinAcl.exe download:
'
http://www.microsoft.com/downloads/details.aspx?FamilyID=e8ba3e56-d8fe-4a91-93cf-ed6985e3927b&displaylang=en

'** Statements:
Dim oShell, WSHShell, oFSO
Dim SRC, KEY, QRY

'** Objects:


Set oShell = Wscript.CreateObject("Wscript.Shell")

Set oFSO = Wscript.CreateObject("Scripting.FileSystemObject")

'** Subinacl.exe location, must be located in the same directory as this
script:
SRC = oFSO.GetFolder(".") & "\subinacl.exe"

'** Type the registry key name here:
KEY = "hkey_current_user\software\keytodelete"

'-----------------------------------------------------------------------------------

'** Take ownership on all keys and values:
oShell.Run "cmd /c " & SRC & " /subkeyreg " & KEY & " /setowner=%username%",
0, True

'** Remove all permissions on all keys and values:
oShell.Run "cmd /c " & SRC & " /subkeyreg " & KEY & " /perm", 0, True

'** Apply current user FC permissions on all keys and values:
oShell.Run "cmd /c " & SRC & " /subkeyreg " & KEY & " /grant=%username%=f",
0, True

'** Delete the key with all subkeys and values:
oShell.Run "cmd /c reg delete " & KEY & " /f", 0, True

'** Check if the key still exist:
QRY = oShell.Run("cmd /c reg query " & KEY, 0, True)
If QRY = "0" Then msgbox KEY & " could not be deleted."
If QRY <> "0" Then msgbox KEY & " is deleted."

'-----------------------------------------------------------------------------------

Phyxious

unread,
Dec 12, 2006, 8:38:01 PM12/12/06
to
But as a domain administrator shouldn't you have full permissions over
everything?

Ayush

unread,
Dec 12, 2006, 11:04:32 PM12/12/06
to

Use WShell.Run to run
reg delete .. .. . .. .

to know about reg delete, Open Command Prompt and type :
reg delete /?

--
Ayush [ Good :-) Luck ]
-------------
Search - www.Google.com | Wikipedia -
http://en.wikipedia.org
Snip your long urls - http://snipurl.com/
-------------

Baard Schøyen

unread,
Dec 13, 2006, 5:24:00 AM12/13/06
to
Hello.

Not necessarily. You can check permissions and ownerships on keys by
right-clicking a key and choose Permissions. In the Permissions dialog, you
can also click Advanced to display the user object who have ownership on the
key.

On a domain member computer, Domain Admins will normally be a member of the
local Administrators group. The domain user (by default member of the
RESTRICTED group) may also need to be a member of the local Administrators
group, if he needs to take ownership and get FC on keys and values. This will
apply if a registry script will be executed under the user's own credentials,
e.g. during login, where the user must have all necessary priveliges.

0 new messages