Search code examples
batch-fileenvironment-variablesregistrykey

Batch file:Setting a registry value to a variable


You think it would easy to google how to set a reg value to a variable in a batch file, but it is consistently different as does not work for my purposes. PowerShell/VBS are out of the question currently and I believe this is possible using a .bat file.

When I use this command:

REG QUERY HKLM\SOFTWARE\Policies\Microsoft\Windows\Windowsupdate /v WUServer

Output:

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Windowsupdate
   WUServer    REG_SZ    http://AAA-WUS-MHQ1234

I just want:

http://AAA-WUS-MHQ1234

and stored as a variable so I can go echo %WSUSServer%

I've tried set and it just sets the command not the output, as well I can't seem to get token, delim, or array to work, but perhaps I am misunderstanding how they work.

Please teach how to use set to make the server a variable.

Many thanks!

Update: So I have tried this, but just in the cmd line and I get %%L not expected...I entered it as one word, does it have to be in a batch file with indents? I wouldn't think so, but am trying now:

FOR /F "usebackq tokens=2,* skip=2" %%L IN ( reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder ) DO SET sdkpath=%%M

Solution

  • You need to do what you've actually said, and use the backqtokens (`). (Also, from the command line you only need one %; in a batch file, you'd use two (%%.)

    FOR /F "usebackq tokens=2,* skip=2" %%L IN (`reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder`) DO SET sdkpath=%%M