We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Search registry for string value and delete all entries found using script or other networking tool

Medium Priority
2,488 Views
Last Modified: 2013-11-30
I want to search the registry for a given string and delete the registry entries that it finds.
E.g. Search for entries with "\\Servername" and delete all the entries that have this in the key/value.
I have a script that will search for the specified string and displays the results in notepad.
I need to be able to then delete all the entries that I have found automatically without any user intervention.
Using script in GPO or using another tool.
A copy of the script is attached in code section

Option Explicit
Dim oWS : Set oWS = CreateObject("WScript.Shell")
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
 
Dim sSearchFor
sSearchFor =  "\\Servername"
 
If sSearchFor = "" Then Cleanup()
 
Dim StartTime : StartTime = Timer
 
Dim sRegTmp, sOutTmp, eRegLine, iCnt, sRegKey, aRegFileLines
 
sRegTmp = oWS.Environment("Process")("Temp") & "\RegTmp.tmp "
sOutTmp = oWS.Environment("Process")("Temp") & "\sOutTmp" & _
          Hour(Now) & Minute(Now) & Second(Now) & ".tmp "
 
oWS.Run "regedit /e /a " & sRegTmp, , True '/a enables export as Ansi for WinXP
 
With oFSO.OpenTextFile(sOutTmp, 8, True)
  .WriteLine("REGEDIT4" & vbcrlf & "; " & WScript.ScriptName & " " & _
    Chr(169) & " Bill James" & vbcrlf & vbcrlf & "; Registry search " & _
    "results for string " & Chr(34) & sSearchFor & Chr(34) & " " & Now & _
    vbcrlf & vbcrlf & "; NOTE: This file will be deleted when you close " & _
    "WordPad." & vbcrlf & "; You must manually save this file to a new " & _
    "location if you want to refer to it again later." & vbcrlf & "; (If " & _
    "you save the file with a .reg extension, you can use it to restore " & _
    "any Registry changes you make to these values.)" & vbcrlf)
 
  With oFSO.GetFile(sRegTmp)
    aRegFileLines = Split(.OpenAsTextStream(1, 0).Read(.Size), vbcrlf)
  End With
 
  oFSO.DeleteFile(sRegTmp)
 
  For Each eRegLine in aRegFileLines
    If InStr(1, eRegLine, "[", 1) > 0 Then sRegKey = eRegLine
    If InStr(1, eRegLine, sSearchFor, 1) >  0 Then
      If sRegKey <> eRegLine Then
        .WriteLine(vbcrlf & sRegKey) & vbcrlf & eRegLine
      Else
        .WriteLine(vbcrlf & sRegKey)
      End If
      iCnt = iCnt + 1
    End If
  Next
 
  Erase aRegFileLines
 
  If iCnt < 1 Then
    oWS.Popup "Search completed in " & FormatNumber(Timer - StartTime, 0) & " seconds." & _
              vbcrlf & vbcrlf & "No instances of " & chr(34) & sSearchFor & chr(34) & _
              " found.",, WScript.ScriptName & " " & Chr(169) & " Bill James", 4096
    .Close
    oFSO.DeleteFile(sOutTmp)
    Cleanup()
  End If
  .Close
 
End With
 
oWS.Popup "Search completed in " & FormatNumber(Timer - StartTime, 0) & " seconds." & _
          vbcrlf & vbcrlf & iCnt & " instances of " & chr(34) & sSearchFor & chr(34) & _
          " found." & vbcrlf & vbcrlf & "Click OK to open Results in WordPad.",, _
          WScript.ScriptName & " " & Chr(169) & " Bill James", 4096
 
oWS.Run "Notepad " & sOutTmp, 3, True
 
oFSO.DeleteFile(sOutTmp)
 
Cleanup()
 
Sub Cleanup()
  Set oWS = Nothing
  Set oFSO = Nothing
  WScript.Quit
End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, if you could post a sample output file that the above code generates, I'm sure we can just run through a series of "Reg Delete" commands to delete the values...

Regards,

Rob.

Author

Commented:
Attached is the output file
Output.txt
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Thanks.....do you want to remove the whole value, or just "empty" the data in it?

If you want the whole value removed, we can change the output file so this it looks like this (for example):

REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\4Developers\RCrawler\Settings]
"LastSearch"=-

If you want to only empty it, we can make it look like this:
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\4Developers\RCrawler\Settings]
"LastSearch"=""

Regards,

Rob.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.