One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .
To access it, you have to click on Edit menu and select Find. The Find box will let you search for items in the Windows Registry, and this includes Keys, Values, and Data. You can also set it to match whole strings only. Hitting the F3 key will take you to all the found searches, one after the other.
By default when a command line execution is completed it should either return zero when execution succeeds or non-zero when execution fails. When a batch script returns a non-zero value after the execution fails, the non-zero value will indicate what is the error number.
Use the GetValue method, specifying the path and name) to read a value from registry key. The following example reads the value Name from HKEY_CURRENT_USER\Software\MyApp and displays it in a message box.
This works for me:
@echo OFF
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
set VALUE_NAME=DefaultColor
FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
if defined ValueName (
@echo Value Name = %ValueName%
@echo Value Type = %ValueType%
@echo Value Value = %ValueValue%
) else (
@echo %KEY_NAME%\%VALUE_NAME% not found.
)
usebackq
is needed since the command to REG QUERY
uses double quotes.
skip=4
ignores all the output except for the line that has the value name, type and value, if it exists.
2^>nul
prevents the error text from appearing. ^
is the escape character that lets you put the >
in the for
command.
When I run the script above as given, I get this output:
Value Name = DefaultColor
Value Type = REG_DWORD
Value Value = 0x0
If I change the value of VALUE_NAME
to BogusValue
then I get this:
"HKEY_CURRENT_USER\Software\Microsoft\Command Processor"\BogusValue not found.
This work for me with variable that contains spaces on Windows 7:
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\Software\SomeAPP" /v ValueName`) DO (
set appdir=%%A %%B
)
ECHO %appdir%
Variable A
contains all data before first space, B
- rest part of ValueName (including further spaces), so appdir = ValueName
This one-liner is pretty much the same as your original try with a couple of additions. It works with paths including spaces, and works in both XP and Windows 7 even if the key is not found (and hides the error). %fn%
will be empty if the key does not exist. This example gets the current desktop background filename:
for /f "tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper 2^>^&1^|find "REG_"') do @set fn=%%b
This command uses tokens=2*
with %%a
as the loop variable but consumes %%b
to correctly handle spaces. When using tokens=2*
, the loop variable %%a
is assigned the value in the second token (in this case, REG_SZ
) and %%b
is assigned the remainder of the line after the next group of delimiter characters, including all internal delimiter characters. This means that %%b
will correctly replicate delimiter characters—even if multiple delimiter characters are clustered together. For example, the value might be C:\A weird path\blah.png
. This technique of reading the value would correctly preserve the two spaces between C:\A
and weird
.
Based on tryingToBeClever solution (which I happened to also stumble upon and fixed myself by trial-and-error before finding it), I also suggest passing the result output of reg query
through find
in order to filter undesired lines due to the ! REG.EXE VERSION x.y
inconsistency. The find
filtering and tokens
tweaking also allows to pick exactly what we want (typically the value). Also added quotes to avoid unexpected results with key/value names containing spaces.
Final result proposed when we are only interested in fetching the value:
@echo off
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKCU\Software\Microsoft\Command Processor
set VALUE_NAME=DefaultColor
for /F "usebackq tokens=1,2,*" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
echo %%C
)
A potential caveat of using find
is that the errorlevel set by reg
when errors occur is now obfuscated so one should only use this approach for keys known to be there and/or after a previous validation.
A tiny additional optimization (add skip=1
to avoid processing the first line of output) can be done in cases when the key name also contains the value name (as it is the case with HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
and CurrentVersion
) but removes most flexibility so should only be used in particular use-cases.
@echo off
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKLM\SOFTWARE\Wow6432Node\Acme Software Inc\Common
set VALUE_NAME=InstallDir
FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B)
echo %pInstallDir%
That works for me in Win7 where the key has a space and the value also has a space. So saving the above in c:\temp as test.bat, open a cmd window and run it.
C:\temp>test
C:\Program Files (x86)\acme Software Inc\APP\
For Windows 7 (Professional, 64-bit - can't speak for the others) I see that REG no longer spits out
! REG.EXE VERSION 3.0
as it does in XP. So the above needs to be modified to use
skip=2
instead of 4 - which makes things messy if you want your script to be portable. Although it's much more heavyweight and complex, a WMIC based solution may be better.
For some reason Patrick Cuff's code doesn't work on my system (Windows 7) probably due to tryingToBeClever's comment. Modifying it a little did the trick:
@echo OFF
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKEY_CURRENT_USER\Software\Microsoft\Command Processor
set VALUE_NAME=DefaultColor
FOR /F "tokens=1-3" %%A IN ('REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul') DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
if defined ValueName (
@echo Value Name = %ValueName%
@echo Value Type = %ValueType%
@echo Value Value = %ValueValue%
) else (
@echo %KEY_NAME%\%VALUE_NAME% not found.
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With