I need help in deleting a registrykey using vbscript...The following
Vbscript deletes only if there are no subkeys under it.But fails when
there are subkeys.
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
Any help in this regards is highly appreciated.
Thanks,
Satish M
It may be possible using available native script functions, but I don't know how
without knowing all the subkeys in advance. Registry functions are sparse in
VBScript. I STRONGLY recommend installing this optional DLL available from
Microsoft. It expands registry handling capability immeasurably. The package
includes full documentation.
http://download.microsoft.com/download/vb60pro/Update/6.0/W9X2K/EN-US/RegObji.exe
With that installed, this script will do what you want:
'------------------KeyKill.vbs-------------------
strKeyPath = "\HKEY_LOCAL_MACHINE\SOFTWARE\System Admin Scripting Guide"
Set objRegistry = CreateObject("RegObj.Registry")
regClear strKeyPath
set objRegistry=nothing
sub regClear(root)
Set objRegKey = objRegistry.RegKeyFromString(root)
Set objRegRootKey = objRegKey.Parent
objRegRootKey.SubKeys.Remove (objRegKey)
Set objRegKey = nothing
Set objRegRootKey = nothing
end sub
'---------------------------------------------------
--
Crash
"History will be kind to me, for I intend to write it."
~ Winston Churchill ~
The script that I'm using is :
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "TestSrv-1"
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "\HKEY_LOCAL_MACHINE\SOFTWARE\System Admin Scripting
Guide"
Set objRegistry = CreateObject("RegObj.Registry")
regClear strKeyPath
set objRegistry=nothing
sub regClear(root)
Set objRegKey = objRegistry.RegKeyFromString(root)
Set objRegRootKey = objRegKey.Parent
objRegRootKey.SubKeys.Remove (objRegKey)
Set objRegKey = nothing
Set objRegRootKey = nothing
end sub
Your help in this regards is higly appreciated.
Thanks,
Satish M
--- snip ---
With the WMI StdRegProv class, the old VB Registry object is really
unnecessary...
StdRegProv class [WMI]
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/stdregprov.asp?frame=true
--
Michael Harris
Microsoft MVP Scripting
No, it has to be installed where the script runs. Try using WMI, as Michael
suggests. (I don't know anything about WMI)
--
Crash
"Dare to be naïve."
~ Buckminster Fuller ~
I am a total newbie at WMI, so cut me some slack, but I can't delete a key and
subkeys without enumerating the entire subtree and deleting the keys one at a
time. In other words, using the documented example, if I create
"Software\MyKey\MySubkey," trying to delete "Software\MyKey" fails. Is there a
way to delete the whole subtree, like there is with the Registry Object?
--
Crash
"Genius is one part inspiration, nine parts perspiration."
~ Alva Thomas Edison ~
Deleting keys with subkeys is more a OS issue that the particular object
model. Earlier Win9x registry 'key with subkey' deletes was supported at
the API level and NT4 and later made you do recursive deletes. Some
registry object models may do the recursive part transparently for you.
RegObj may be one of those - I haven't used it in years, probably 1998/99...
> Deleting keys with subkeys is more a OS issue that the particular object
> model. Earlier Win9x registry 'key with subkey' deletes was supported at
> the API level and NT4 and later made you do recursive deletes. Some
> registry object models may do the recursive part transparently for you.
> RegObj may be one of those - I haven't used it in years, probably 1998/99...
Hey, ANY script does a lot of low level stuff transparently! That's what makes
it different from Assembly! :-)
The RegObj script I posted works with W2K Pro.
Anyway, I take it the answer is no, that I would have to build a recursive loop
to delete an entire subtree using native WMI methods.
--
Crash
Yes you would when using StdRegProv but it really isn't that hard...
Google Groups: microsoft.public.scripting.wsh
Deleting Registry KEY with Subkeys
http://groups.google.com/group/microsoft.public.scripting.wsh/msg/3091255b82fecfab
(I can't believe I posted that 5 years ago!)