Solved

Delete Value and keys from the Registry using VBscript

Posted on 2014-10-22
18
445 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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
 

Author Comment

by:Vicki05
Comment Utility
Does not work
0
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
Comment Utility
It runs fine but does not delete the path or the sub keys
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:becraig
Comment Utility
Are you running as Administrator ?
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
I am looking for a vbscript that will delete digest folder and all the subkeys.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now