search for reg key containing string

Microsoft Windows 2003 server - 5 users
May 21, 2010 at 10:21:58
Specs: windows server 2003 and up
I am trying to create a bat file that will search a registry hive and return a key that contains a substring ...and doesn't contain another substring....

I need to search and find certain key names but do not have the full key name ...\
is there a way to do this with REG.exe

See More: search for reg key containing string

Report •

May 21, 2010 at 10:40:13
If you can show how to do the reg query, I may be able to help with the bat.

Helping others achieve escape felicity


Report •

May 21, 2010 at 18:58:11
Maybe something like this:

for %%a in ("HKLM" "HKCU" "HKCR" "HKU" "HKCC") do (
    2>nul reg query %%a /s | find /v "    " | find /i "wanted string" | find /v /i "unwanted string"

Remove any unneeded hive roots to taste, or add subkeys
to narrow the search.

Remove /i switches to the find commands for a case sensitive search.

Report •

May 21, 2010 at 19:22:29
thank you so much for the replies.... I had some success
running Reg Query and piping to FINDSTR and this works
great for obtaining the key names...
Now i am trying to figure out how i can save that key name in
a variable so that i can check for the existence of a vlaue and
retreive the data in the value ( they are all REG_SZ ) for the
moment I guess this qualifies as another question :)
Also, I guess I need some help with For loops in bat files...
in for %%a in ("HKLM" "HKCU" "HKCR" "HKU" "HKCC") do (
2>nul reg query %%a /s | find /v " " | find /i "wanted
string" | find /v /i "unwanted string"
i don't understand what the %%a and the 2>nul means

Report •

Related Solutions

May 21, 2010 at 20:50:07
2>nul = Disregard all error messages from this command. I only did this because reg will probably hit some keys you don't have permission to access.

%%a = A for loop variable. It gets replaced with every iteration.

The trick to getting the output of a command into a variable is to use a for /f loop. Can you post the command you intend to capture the output of(I'm not sure if your using what I posted or not)?

Depending on the circumstances you may need to use a pseudo array of variable or a couple of nested for loops.

Report •

May 22, 2010 at 10:23:29
that is a great reference! will take me some time to work it
through and really ingest it tho :)

my ultimate goal is to read the reg key name..., i attain with
reg query|FINDSTR [save the key name in a variable) then
read the data stored in a value ( If value exists) and save that
data in a variable. after i run the script, I want to write the
variables and data into a txt file ( which will be consumed by
another program) in the form of %VAR% = %DATA%
I am searching HKLM\Software\Key for subkeys
HKLM\System\Servies\servicename and

and I have dual purposes... one to verify an install in which
case i can grab entire keys and subkeys, and the other is to
consume values for use by another program... and i cannot
be sure powershell is installed on the that is
why i am looking into bat files.

You have been very helpful...learning learning learning :)
thanks so much

Report •

May 22, 2010 at 20:47:32
I'm sure there is an easier way, I'm just still a little blurry
on the exact requirements...

@echo off
SetLocal EnableDelayedExpansion
rem keys must not end with \ to subrecurse
set set1="HKLM\Software\Key","search string","valuename"
set set2="HKLM\System\Servies\servicename","search string","valuename"
set set3="HKLM\software\Microsoft\Windows\Currentversion\Uninstall\keyname","search string","valuename"
set set=3

set dataout=0
set fail=0

for /l %%a in (1 1 %set%) do (
    for /f "tokens=1-3 delims=," %%b in ("!set%%a!") do (
        set reason=key.
        for /f "delims=" %%e in ('2^>nul reg query %%b /s ^| find /v "    " ^| 2^>nul FINDSTR /c:%%c^| FINDSTR /c:%%c ') do (
            set reason=value.
            for /f "skip=1 tokens=2* delims=	" %%f in (' 2^>nul reg query %%e /v %%d ') do ( 
                set /a dataout+=1
                set key!dataout!=%%e
                set data!dataout!=%%g
        set /a testfail=dataout + fail
        if !testfail! neq %%a (
            set /a fail+=1
            1>&2 echo FailedSet: !set%%a!
            1>&2 echo Reason: could not find !reason!

Rem This would be the file output
for /l %%a in (1 1 %dataout%) do (
    echo !key%%a! = !data%%a!

Report •

Ask Question