Solved

Delete Value and keys from the Registry using VBscript

Posted on 2014-10-22
18
487 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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