Use .vbs to delete registry keys for HKEY USER

BatchPatch Forums Home Forums BatchPatch Support Forum Use .vbs to delete registry keys for HKEY USER

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #11968
    Tomputer
    Participant

    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.

    #11969
    doug
    Moderator

    To 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-stdregprov

    #11970
    Tomputer
    Participant

    Thank 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.

    #11973
    doug
    Moderator

    I 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

    #11975
    Tomputer
    Participant

    Thank you for your help
    I checked it again on different computers and script works correct.

    #12141
    RAJA1266
    Participant

    Hi 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)) Then

    modified:

    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

    #12142
    doug
    Moderator

    1. 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.

    #12143
    RAJA1266
    Participant

    It 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

    #12144
    doug
    Moderator

    Your 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.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.