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

Windows Scripting won't delete registry keys.

5 views
Skip to first unread message

Jon Price

unread,
Dec 2, 2003, 11:11:38 AM12/2/03
to
I've created a script that enumerates a few registry keys
and then is supposed to delete them. I've checked to make
sure that I've rights to actually delete the key, however,
still not able to delete them successfully. (the reason
I'm using a script is that the uninstall doesn't work for
the application and the developers suggested all my users
to manually go an delete registry keys... I would rather
control this with a script). Below is an example of my
script... anyone know why I wouldn't be able to delete
all the keys that I search for (and find)?

========== script start ==========

On Error Resume Next

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000004
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7

' Set to the root directory for the application.
strApplicationPath = "C:\path\to\application\directory"

Wscript.Echo "WARNING: Using this script edits the
Microsoft Windows registry and deletes files from the
local hard drive [" & strApplicationPath & "]. This may
cause serious problems that could require you to reinstall
Windows. Use this script at your own risk."

' Cleans registry of application references.
Call EnumRegistry
(HKEY_CLASSES_ROOT, "Installer\Products", "", "Application
Module", REG_SZ)
Call EnumRegistry
(HKEY_CLASSES_ROOT, "Installer\Products", "", "Application
Core Client", REG_SZ)
Call EnumRegistry
(HKEY_CLASSES_ROOT, "Installer\Products", "", "Application
Corr Module", REG_SZ)
Call EnumRegistry
(HKEY_CLASSES_ROOT, "Installer\Products", "", "Application
Name", REG_SZ)
Call EnumRegistry
(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVer
sion\Installer\UserData\S-1-5-18
\Products", "InstallProperties", "Application Name",
REG_SZ)
Call EnumRegistry
(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVer
sion\Uninstall", "", "Application Name", REG_SZ)

' Deletes root directory of application.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(strApplicationPath)

Wscript.Echo "DONE: Please reboot the workstation before
you re-install the application!"

Function EnumRegistry(HKEY_BASE, strKeyPath,
strSubKeyPath, strSearchValue, bytDataType)
strComputer = "."
Set objReg=GetObject("winmgmts:
{impersonationLevel=impersonate}!\\" & strComputer
& "\root\default:StdRegProv")
objReg.EnumKey HKEY_BASE, strKeyPath, arrSubKeys
For Each SubKey in arrSubKeys
If IsNull (strSubKeyPath) Then
strFullKeyPath =
strKeyPath & "\" & SubKey
Else
strFullKeyPath =
strKeyPath & "\" & SubKey & "\" & strSubKeyPath
End If
strDelKeyPath = strKeyPath & "\" &
SubKey
objReg.EnumValues HKEY_BASE,
strFullKeyPath, arrEntryNames, arrValueTypes
If IsNull (arrEntryNames) Then
' The array has a Null
value, nothing to do.
Else
For i = 0 to UBound
(arrEntryNames)
Select Case
arrValueTypes(i)
Case
bytDataType

objReg.getStringValue HKEY_BASE, strFullKeyPath,
arrEntryNames(i), strValue
If
strValue = strSearchValue Then

Wscript.Echo strSearchValue & " Found!"

Wscript.Echo "Attempting to Delete Key: " &
strDelKeyPath

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

'
Check for delete access rights on key.

'objReg.CheckAccess HKEY_BASE, strDelKeyPath,
&H00010000, bHasAccessRight

'If bHasAccessRight = True Then
'
Wscript.Echo "Delete Access Right: True."

'Else
'
Wscript.Echo "Delete Access Right: False."

'End If


objReg.DeleteKey HKEY_BASE, strDelKeyPath
End If
End Select
Next
End If
Next
End Function

Torgeir Bakken (MVP)

unread,
Dec 2, 2003, 12:57:20 PM12/2/03
to
Jon Price wrote:

> I've created a script that enumerates a few registry keys
> and then is supposed to delete them. I've checked to make
> sure that I've rights to actually delete the key, however,
> still not able to delete them successfully. (the reason
> I'm using a script is that the uninstall doesn't work for
> the application and the developers suggested all my users
> to manually go an delete registry keys... I would rather
> control this with a script). Below is an example of my
> script... anyone know why I wouldn't be able to delete
> all the keys that I search for (and find)?

Hi

To delete a key that have subkeys, you need to use use a recursive sub (a sub
that calls itself) to delete the key(s):


Const HKLM = &H80000002
sKey = "System\CurrentControlSet\Services\Myservice"

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

DeleteRegistryKey HKLM, sKey


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


0 new messages