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

7

3

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.

EDIT

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.

John

Posted 2015-11-03T18:54:40.273

Reputation: 73

What makes you sure the delimiter is tab? On my Win8.1 it's several spaces (looks like 4). – dave_thompson_085 – 2015-11-04T02:26:18.693

I typed "tab" in notepad. When I copy from notepad to here, the tab turns into these 4 spaces. – John – 2015-11-04T11:22:45.877

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. – dave_thompson_085 – 2015-11-05T16:21:30.207

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 – 2015-11-08T18:15:47.437

Answers

5

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

DaveO

Posted 2015-11-03T18:54:40.273

Reputation: 86

Thank you very much! It worked. I thought the reg query output was a tab. – John – 2015-11-08T18:18:10.583

2

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

Mark

Posted 2015-11-03T18:54:40.273

Reputation: 21

2

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

DaveO

Posted 2015-11-03T18:54:40.273

Reputation: 86

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 – 2015-11-04T11:57:05.530