Home » MSI (Windows Installer) » Useful Scripts for Custom Actions » Delete a Registry Key and its Subkeys

Delete a Registry Key and its Subkeys

Posted on by

Description:

Useful when we want to delete registry which has been left behind post uninstall.

Source:

technet.microsoft.com

Script:

Option Explicit

Dim intHive
Dim strComputer
Dim strKeyPath
Dim strSubkey 
Dim arrSubkeys

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005

On Error Resume Next 

strComputer = "."
strKeyPath = "Software\Test" 

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

DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath 

Set objRegistry  = Nothing

Sub DeleteSubkeys(HKEY_HIVE, strKeyPath) 
    objRegistry.EnumKey HKEY_HIVE, strKeyPath, arrSubkeys 

    If IsArray(arrSubkeys) Then 
        For Each strSubkey In arrSubkeys 
            DeleteSubkeys HKEY_HIVE, strKeyPath & "\" & strSubkey 
        Next 
    End If 

    objRegistry.DeleteKey HKEY_HIVE, strKeyPath 
End Sub

 

 

Comments have now been disabled. If you have a question to ask about this post please ask the community!