Solved

find and delete registry strings in keys batch file

Posted on 2014-04-11
8
6,949 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
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 53

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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 53

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 53

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
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!
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

829 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