Solved

Delete Value and keys from the Registry using VBscript

Posted on 2014-10-22
18
479 Views
Last Modified: 2014-11-18
I need to delete registry key and subkeys. Could someone help me with a vbscript that would do that. The folder I am trying to delete is
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Digest folder and its contents.

I--New-Folder--2--untitled2.bmp
0
Comment
Question by:Vicki05
  • 9
  • 7
18 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40397745
Here  is a good script for this:

On Error Resume Next 

Const HKEY_CURRENT_USER = &H80000001 

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

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

DeleteSubkeys HKEY_CURRENT_USER, strKeypath 

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

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

    objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath 
End Sub

Open in new window


The concept is, it deletes each subkey first before deleting the key you want to have removed.

I did take this from technet.
http://technet.microsoft.com/en-us/magazine/2006.08.scriptingguy.aspx
0
 

Author Comment

by:Vicki05
ID: 40397870
I tried that but it does not seem to work.

On Error Resume Next

Const HKU = &H80000003

strComputer = "."
strKeyPath = "\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Digest"

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

DeleteSubkeys HKU, strKeypath

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

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

    objRegistry.DeleteKey HKU, strKeyPath
End Sub



msgbox "Done"
0
 
LVL 29

Expert Comment

by:becraig
ID: 40397969
ok so it looks like you missed the full path for the reg key.

On Error Resume Next 

Const HKEY_USERS = &H80000003

strComputer = "."
strKeyPath = ".DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Digest" 

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

DeleteSubkeys HKEY_USERS, strKeypath 

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

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

    objRegistry.DeleteKey HKEY_USERS, strKeyPath 
End Sub

Open in new window

0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:Vicki05
ID: 40397998
Does not work
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40398021
What error do you get ?

I just tested this on my system and it works.

You intially had the wrong registry path:
strKeyPath = "\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Digest"
instead of
strKeyPath = ".DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Digest"

Can you give me an idea of what is not working ?

It is hard to figure out since it works for me on my system:
C:\Users\Administrator\Desktop>reg query "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings
    User Agent    REG_SZ    Mozilla/4.0 (compatible; MSIE 8.0; Win32)
    IE5_UA_Backup_Flag    REG_SZ    5.0
    ZonesSecurityUpgrade    REG_BINARY    F5511E7B469FCE01
    EnableNegotiate    REG_DWORD    0x1
    ProxyEnable    REG_DWORD    0x0

HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\5.0
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Lockdown_Zones
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones

C:\Users\Administrator\Desktop>cscript delreg.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Users\Administrator\Desktop>reg query "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
ERROR: The system was unable to find the specified registry key or value.

Open in new window

0
 

Author Comment

by:Vicki05
ID: 40398033
It runs fine but does not delete the path or the sub keys
0
 
LVL 29

Expert Comment

by:becraig
ID: 40398048
Can you simply open up  regedit and right click on the key an copy key name.
Then simply replace
strKeyPath = ".DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Digest"

with
strKeyPath = "<whatever the value is that you get>"

it would be unusual to work for me and not for you unless you are not running your command prompt as administrator.
0
 

Author Comment

by:Vicki05
ID: 40398064
I am using your edited script. I even tried creating keys. It still does not delete. This is Windows XP computer. I changed digest to Lock


On Error Resume Next

Const HKEY_USERS = &H80000003

strComputer = "."
strKeyPath = ".DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Lock"

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

DeleteSubkeys HKEY_USERS, strKeypath

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

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

    objRegistry.DeleteKey HKEY_USERS, strKeyPath
End Sub

msgbox "Done"
0
 
LVL 29

Expert Comment

by:becraig
ID: 40398078
Are you running as Administrator ?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40398118
why not make it simple then:

Just run the following command.


reg delete "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Lock"  /f
0
 

Author Comment

by:Vicki05
ID: 40399416
Lock was used as a test. I need to delete the digest folder and all the things underneath it.

strKeyPath = "\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Digest"
0
 
LVL 29

Expert Comment

by:becraig
ID: 40400430
Ok so my last comment was to simply use the reg delete command, did you see or try that ?

reg delete "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Lock"  /f
0
 

Author Comment

by:Vicki05
ID: 40400492
Yes I did, but it did not work since there is subkeys and another folder under Digest folder. I am not trying to delete the lock folder. I tried again to delete the lock folder using what you gave. This is what I got. I used WshShell

Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegDelete "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Lock"  /f

I--untitled.bmp
0
 
LVL 29

Expert Comment

by:becraig
ID: 40400513
So I think we mixed two things here
reg delete "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Lock"  /f  

Open in new window

was just meant to be a windows command that would run from a command prompt, not to be inserted into the VBscript.

This is a command that should be able to run on its own and delete the entire key.
0
 

Author Comment

by:Vicki05
ID: 40406337
I am looking for a vbscript that will delete digest folder and all the subkeys.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40450908
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 
LVL 29

Expert Comment

by:becraig
ID: 40450909
The comment: http:#a40398021

Provided a proven working example of how to accomplish this, the author was unable to provide any specific details as to why this did not work on their system.

In a nutshell the script does exactly what the author requested and was tested with posted results,
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question