I would like to delete the following value recursively through the User Profiles/HKEY_USERS location. This is what I got so far. However I cannot get the script to delete the value under "strKeyPath" variable. I have removed what I did before as it was very wrong.
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
'Set the local computer as the target
strComputer = "."
'set the objRegistry Object
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
'Enumerate All subkeys in HKEY_USERS
objRegistry.EnumKey HKEY_USERS, "", arrSubkeys
'Define variables
strKeyPath = "\Software\Microsoft\Windows\CurrentVersion\Uninstall\50cc940dd0f54608"
strSID = "S-1-5-21-\d*-\d*-\d*-\d*\\"
What you're trying to achieve isn't really 'recursion', you're just deleting a registry key from a number of different places. This is how I'd do it:
Option Explicit
Const HKU = &H80000003
Dim wmi, reg
Dim prof, profs
Dim key
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set reg = GetObject("winmgmts:\\.\root\default:StdRegProv")
Set profs = wmi.ExecQuery("Select SID from Win32_UserProfile where SID like 'S-1-5-21%'")
For Each prof In profs
key = prof.SID & "\Software\Microsoft\Windows\CurrentVersion\Uninstall\50cc940dd0f54608"
WScript.Echo key
'Call reg.DeleteKey(HKU, key) 'commented out for safety
Next
The WMI query matches all user profiles on the system, the same as your regular expression, and the result of it is used to build a registry path.
Please be careful when deleting from the registry