How to get a registry value and set into a variable in batch

You don't need the delims switch, at all, since the default is space, which is what the reg query is returning. In making a bat file for this for loop and registry on a key that I am messing with I get the correct echo, for my instance the "Red" value of the RGB Background color is 55:

for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\Colors"  /V Background  ^|findstr /ri "REG_SZ"') do echo %%a

The approved answer is not correct in some situations - if value read from the registry containst white characters i.e. spaces (Program Files (x86)) then it returns only the first part of the value ('Program'). What is I worked out is:

FOR /F "tokens=2* skip=2" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion" /v "CommonFilesDir"') do echo %%b

The result is C:\Program Files\Common Files


The syntax of the DOS command is correct. I would question whether you have the correct registry key value. Just type the req query... part into the command line and see what is returns. I am running Win 7 and I do not find the key , LastUsedUsername, defined in HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon