Solved

find and delete registry strings in keys batch file

Posted on 2014-04-11
8
7,065 Views
Last Modified: 2014-11-05
I have a need to delete any strings in a certain registry key location that has the word redirect in it.  Batch file is preferable but VB is welcomed.  
example;
reg key location
hkcu\software\Microsoft\windows NT\devices\
string value (any string with the below in the value)
redirect
0
Comment
Question by:jo80ge121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 92

Expert Comment

by:nobus
ID: 39995732
i use regseeker for that : http://www.hoverdesk.net/
0
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 39995921
Try the script below; it's currentyl in test mode and will only display the "reg delete" command(s) it would normally run, so that you can test it. To run it for real, remove the uppercase ECHO in line 7. The correct registry location is actually "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices", btw.
@echo off
setlocal enabledelayedexpansion
set Key=HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices
set Find=*redirect*
for /f "delims=" %%a in ('reg.exe query "%Key%" /v "%Find%" ^| find "REG_SZ"') do (
	call :GetValueName Value "%%a"
	ECHO reg.exe delete "%Key%" /v "!Value!" /f
)	

goto :eof
:GetValueName
set Return=
for %%a in (%~2) do (
	if "%%a"=="REG_SZ" (
		set %1=!Return:~1!&goto :eof
	) else (
		set Return=!Return! %%a
	)
)
goto :eof

Open in new window

0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 39996146
Building off oBdA answer to handle the leading white spaces in the value names, and elliminate the subroutine call (it's really hard to beat oBdA to these questions, too fast! :-] ) here's a slightly different but effective approach.

@echo off
setlocal EnableDelayedExpansion

REM Define registry location and pattern to look for
set "Key=HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices"
set "Find=*redirect*"

REM locate the registry values we are interested in deleting
for /f "delims=" %%a in ('reg.exe query "%Key%" /v "%Find%" ^| find "REG_SZ"') do (
    REM Trim off leading spaces from value name
    for /f "tokens=* delims= " %%b in ("%%a") do set "Name=%%b"
    REM Break apart REG QUERY output at REG_SZ literal
    set "Name=!Name:    REG_SZ=@!"
    for /f "delims=@" %%b in ("!Name!") do (
        REM Delete this registry value
        ECHO reg.exe delete "%Key%" /v "%%b" /f
        echo Deleted: "%%b"
    )
)

Open in new window

~bp
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Closing Comment

by:jo80ge121
ID: 39996628
both of them worked.  thank you!
0
 
LVL 1

Expert Comment

by:mcburn13
ID: 40422200
not working for me   the Find variable *s mean that it's wildcard before and after the term right?  Anyway just getting an error even on running the exact string value:
C:\Scripts>reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "\\test\test" | find "REG_SZ"
ERROR: The system was unable to find the specified registry key or value.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 40422750
Can you do the following at a command prompt and post the results.  Based on what you described it does not seem that the KEY named "\\test\test" exists, which I would not expect it to.  The key names are typically reserved works / names like "AppData", "Desktop", "Start Menu", etc.

Anyway, you can check this by the following:

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

and see what keys are listed.

~bp
0
 
LVL 1

Expert Comment

by:mcburn13
ID: 40423949
I was actually able to get it going with this syntax:
SET KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
SET VALUE=\\OLDSERVERNAME*
for /F "tokens=1,*" %%a in ('REG QUERY "%KEY%" ^| findstr /I "%VALUE%"') do (REG DELETE "%KEY%" /v %%a /f)

I had to specify the \\ in the UNC path it didn't like a preceding wildcard in the variable.  BUT I would like to figure out how to get that preceding wildcard working for future situations (doesn't apply here because this string in this instance will always start with \\OLDSERVERNAME)
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 40425437
The leading asterisk definitely works, as in this test I just did:

C:\> reg.exe query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices" /v "*envy*"

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices
    HP ENVY 120 series (Network)    REG_SZ    winspool,Ne01:

~bp
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

730 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