I need to get a value in a registry key and store in a variable using a batch file.

I wrote a basic command line to exemplify my logic (using echo instead of setting a variable):

for /f "tokens=3 delims=    " %%a in ('reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "LastUsedUsername" ^|findstr /ri "REG_SZ"') do echo=%%a

I expect the username to be printed in the screen, but it doesn't happen.

I am sure the Registry value "LastUsedUsername" is not empty, it really has data. Also, the delimiter is a tab, not spaces.


If I just type

reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "LastUsedUsername"

... it returns:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon
    LastUsedUsername    REG_SZ    Administrador

This code

reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "LastUsedUsername" ^| findstr /ri "REG_SZ"

... returns:

    LastUsedUsername    REG_SZ    Administrador

Then, when I use the for command, I just get no output from echo.

  • What makes you sure the delimiter is tab? On my Win8.1 it's several spaces (looks like 4). Nov 4, 2015 at 2:26
  • I typed "tab" in notepad. When I copy from notepad to here, the tab turns into these 4 spaces.
    – John
    Nov 4, 2015 at 11:22
  • John: I mean on 8.1 when I do the reg query the output uses spaces not tab, as @DaveO v2 agrees. Parsing with delims=(tab) doesn't work, parsing with the default does. Nov 5, 2015 at 16:21
  • Sorry, I didn't get it. You and he are correct. I have an very old .bat script that uses tab as delimiter and it still works even on my Windows 8. Thinking about why it works and my script doesn't, I guess it's because the reg.exe (used in the old script) is an older version than Windows 8 native reg.exe. Thank you!
    – John
    Nov 8, 2015 at 18:15

4 Answers 4


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
  • Thank you very much! It worked. I thought the reg query output was a tab.
    – John
    Nov 8, 2015 at 18:18
  • Note that this will not work as expected if there are white-space characters in the value. See superuser.com/a/1488030/1640438
    – Simon
    Feb 24 at 13:37

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

  • Good catch. But when querying the path variable, it prints it twice for some reason. The workaround I found was to use @echo FOR /F "skip=2 tokens=2*" %a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v path') do @echo %b. set doesn't have the same issue though.
    – Amit Naidu
    Mar 12, 2021 at 1:47
  • This answer is broken, not only it says %%a in one spot and then %%b in another, but it also returns REG_SZ instead of value. Dec 15, 2021 at 15:42
  • 2
    @IgorLevicki The answer isn't broken, but you did successfully manage to break it. If you had just left it with %%b at the end, you would have gotten the correct response. When executing a for /f loop with the tokens option set to retrieve multiple values (like tokens=2,* meaning get the second part and then everything that comes after — the comma may be omitted as in the answer), cmd.exe populates the specified variable with a one-character name and the required number of extra variables with consecutive characters as names for the extra values requested (here, the single %%b).
    – Taederias
    Jan 6 at 1:03
  • @Taederias I stand corrected, I am using batch scripts a lot and I didn't know about that. I think that it would be worth explaining / mentioning that in the answer itself. Jan 6 at 18:06
  • @IgorLevicki Yeah, the shell just wantonly assigning variables does appear awkward and weird, especially from the perspective of someone used to “proper” programming languages. Here it appears as a sort of workaround for batch files having no concept of arrays, or really any complex data structures. (Side note though: when one thinks a piece of code is incorrect, it is generally prudent to at least try out said code as provided before making such claims.)
    – Taederias
    Jan 6 at 22:37

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

  • I edited the question to add more info and answer you better. According to the output, the registry key value is correct. Anyway, I changed the value "LastUsedUsername" to "Shell". I should get "explorer.exe" as output, but I didn't. So, I believe something is wrong in FOR command
    – John
    Nov 4, 2015 at 11:57

setx can do what you want, no need for a script:

setx DefaultUserName /k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName"

Note: The variable can't be used in the current command script as it is set in the user's environment, not in the current environment. Check with

echo %DefaultUserName% (doesn't work OOTB but will work in a new cmd window)
reg query "HKCU\Volatile Environment" /v "DefaultUserName" (doesn't work)
reg query "HKCU\Environment" /v "DefaultUserName" (works)
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 17 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.