BatchPatch Forums Home › Forums › BatchPatch Support Forum › Use .vbs to delete registry keys for HKEY USER
- This topic has 8 replies, 3 voices, and was last updated 5 years, 1 month ago by doug.
-
AuthorPosts
-
September 30, 2019 at 6:11 am #11968TomputerParticipant
Hi.
I used your tutorial how to “Deploying a Registry Key / Value to HKEY_CURRENT_USER (HKCU) or All Users in HKEY_USERS (HKU)”
And it’s work perfect. Now I can deploy new keys for current loge users, I was looking for that solution for 3 days :).
I have question if it is possible to use this code to delete all registry keys/subkeys from specified Registry strRegPath. I tried to modify this .vbs code but it’s not work.
September 30, 2019 at 1:53 pm #11969dougModeratorTo delete a single registry key on target computers you could use the syntax described in this posting: deleting-registry-key
If you are trying to delete a key from all users in HKU then yes you could certainly modify the script at this link: deploying-a-registry-key-value-to-hkey_current_user-hkcu-or-all-users-in-hkey_users-hku That script currently uses StdRegProv.EnumKey, StdRegProv.CreateKey, and StdRegProv.SetStringValue. You could modify the script to fit your needs and to use StdRegProv.DeleteKey as the deletion method.
More on the aforementioned StdRegProv methods:
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/regprov/enumkey-method-in-class-stdregprov
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/regprov/createkey-method-in-class-stdregprov
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/regprov/setstringvalue-method-in-class-stdregprov
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/regprov/deletekey-method-in-class-stdregprovOctober 1, 2019 at 2:24 am #11970TomputerParticipantThank you very much for your response.
I try to delete all keys from registry path.
Can’t use one key delete because the name is not the same, one in most cases the key name is different.I modified this code in that way:
strComputer = "." strRegPathSuffix = "\Software\Microsoft\Office\16.0\Outlook\Resiliency\DisabledItems" Const HKEY_USERS = &H80000003 Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "" oReg.EnumKey HKEY_USERS, strKeyPath, arrSubKeys For Each subkey In arrSubKeys 'wscript.echo subkey If NOT ((InStr(1,subkey,".DEFAULT",1) > 0) OR (InStr(1,subkey,"S-1-5-18",1) > 0) OR (InStr(1,subkey,"S-1-5-19",1) > 0) OR (InStr(1,subkey,"S-1-5-20",1) > 0) OR (InStr(1,subkey,"_Classes",1) > 0)) Then strKeyPath = subkey & strRegPathSuffix 'wscript.echo strKeyPath oReg.DeleteKey HKEY_USERS, strKeyPath End If Next
After run this script I did not get any errors but also not deleting any keys form specified registry path.
October 1, 2019 at 3:36 pm #11973dougModeratorI don’t know why you are having problems, but I would suggest you test your script directly on a computer without using BatchPatch. Then you can add some error handling to figure out exactly why it’s failing. For example if you run the following script it will print out the return code and error code if it fails:
strComputer = "." strRegPathSuffix = "\Software\Microsoft\Office\16.0\Outlook\Resiliency\DisabledItems" Const HKEY_USERS = &H80000003 Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "" oReg.EnumKey HKEY_USERS, strKeyPath, arrSubKeys For Each subkey In arrSubKeys 'wscript.echo subkey If NOT ((InStr(1,subkey,".DEFAULT",1) > 0) OR (InStr(1,subkey,"S-1-5-18",1) > 0) OR (InStr(1,subkey,"S-1-5-19",1) > 0) OR (InStr(1,subkey,"S-1-5-20",1) > 0) OR (InStr(1,subkey,"_Classes",1) > 0)) Then strKeyPath = subkey & strRegPathSuffix wscript.echo strKeyPath Return = oReg.DeleteKey(HKEY_USERS, strKeyPath) If (Return = 0) And (Err.Number = 0) Then Wscript.Echo HKEY_USERS & strKeyPath & " successfully deleted" Else Wscript.Echo ".DeleteKey failed. Return value = " & Return & ". Error value = " & Err.Number End If End If Next
You can then lookup the return code at this link. Note, the enum is zero based, so wbemNoErr == 0, wbemErrFailed == 1, wbemErrNotFound == 2, and so on: https://docs.microsoft.com/en-us/windows/win32/api/wbemdisp/ne-wbemdisp-wbemerrorenum?redirectedfrom=MSDN
October 2, 2019 at 1:00 am #11975TomputerParticipantThank you for your help
I checked it again on different computers and script works correct.November 20, 2019 at 6:35 am #12141RAJA1266ParticipantHi Dough,
Above script is cool.
I need to delete a key under software node irrelevant of any SID so I just changed if not to IF
But the script run and does not delete anything
Original:
If NOT ((InStr(1,subkey,”.DEFAULT”,1) > 0) OR (InStr(1,subkey,”S-1-5-18″,1) > 0) OR (InStr(1,subkey,”S-1-5-19″,1) > 0) OR (InStr(1,subkey,”S-1-5-20″,1) > 0) OR (InStr(1,subkey,”_Classes”,1) > 0)) Thenmodified:
If ((InStr(1,subkey,”.DEFAULT”,1) > 0) OR (InStr(1,subkey,”S-1-5-18″,1) > 0) OR (InStr(1,subkey,”S-1-5-19″,1) > 0) OR (InStr(1,subkey,”S-1-5-20″,1) > 0) OR (InStr(1,subkey,”_Classes”,1) > 0)) Then
I get .DeletKey Failed. Return value = 2. Error Value = 0
Could you please help
November 20, 2019 at 1:29 pm #12142dougModerator1. Your modification to the script is wrong. You should either leave the original If/Then statement as-is, or you should remove the entire If/Then statement. However, your modification to the If/Then statement will produce a result that is not aligned with what you are aiming to do.
2. You can lookup the return code at this link. Note, the enum is zero based, so wbemNoErr == 0, wbemErrFailed == 1, wbemErrNotFound == 2, and so on: https://docs.microsoft.com/en-us/windows/win32/api/wbemdisp/ne-wbemdisp-wbemerrorenum?redirectedfrom=MSDN
You received a return code of 2, indicating that you are trying to delete a reg key that does not exist. This is not surprising because the way that you modified the script would cause the script to the look for the key in places where it would not likely exist.
November 20, 2019 at 1:36 pm #12143RAJA1266ParticipantIt does exist that is reason why I chose to removed if not and replaced with if.
RightFax application does have the registries under default and s–18 profiles.
I ran this on machine where it had keys software\RightFax client
November 20, 2019 at 1:46 pm #12144dougModeratorYour script with the modified If/Then statement would try to remove the key from each of
.DEFAULT, S-1-5-18, S-1-5-19, S-1-5-20. If the key does not exist in at least one of those places then I would expect it to return 2. If you want to delete the key from specific locations, then modify the script to just delete from those locations rather than leaving the script to loop through all of the subkeys. -
AuthorPosts
- You must be logged in to reply to this topic.