Search code examples
batch-filewindows-10registry

How can I output only the value of a registry key in a Batch File?


Is it possible to get only the value of a registry key? (Windows 10)

When I run reg query "HKCU\Control Panel\Desktop" /v Wallpaper in a batch file

I get the output Wallpaper REG_SZ c:\windows\web\wallpaper\windows\img0.jpg

Is it possible to make the file only output c:\windows\web\wallpaper\windows\img0.jpg?


Solution

  • For this specific task, you simply need to isolate part of the returned strings.

    You can do that from a using a For loop; like this:

    @For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query
     "HKCU\Control Panel\Desktop" /V Wallpaper 2^>NUL') Do @Echo %%H
    

    To learn how to use a for loop, open a Command Prompt window, type for /?, and press the ENTER key.


    If you intend to further use that string, you may be better advised to save it as a variable instead:

    @For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query
     "HKCU\Control Panel\Desktop" /V Wallpaper 2^>NUL') Do @Set "img=%%~H"
    

    You can then use %img% or "%img%" as necessary, elsewhere in your script, in place of the returned value string.

    To learn how to use the set command, open a Command Prompt window, type set /?, and press the ENTER key.