Read Registry value into a batch variable

Modified: 2017/06/25

Rue-Sainte-Catherine--2015-Before I forget, let me share with you a fantastic way to read values from Registry into a batch file variable.

Say you need to determine, what is the date format of this machine.  Normally you would read a value from HKEY_CURRENT_USER\Control Panel\International like this:

REG QUERY "HKCU\Control Panel\International" /v sShortDate


That command will produce an output similar to this:

HKEY_CURRENT_USER\Control Panel\International
sShortDate    REG_SZ    M/d/yyyy

But you do not need all that.  All you need is the string  “M/d/yyyy” (or “yyyy-MM-dd”).  To get just that string you need to skip to a second line and read third field on the last line.  How you can do that in a batch file?

Look at this batch command below.  It does just that:

FOR /F "skip=2 tokens=2,*" %%A IN ('reg.exe query "HKCU\Control Panel\International" /v "sShortDate"') DO set "DFMT=%%B"
ECHO New variable DFMT = %DFMT%

This amazing command sets variable DFMT to a value stored in REG key sShortDate.  It skips two lines (skip=2).  It uses default delimiter space (no code is needed).  It ignores first two fields (tokens=2,*), and puts everything that remains into %B.

Note: Syntax tokens=2,3 would also work in this case because output doesn’t contain any spaces.  This would mean, skip first two fields and use third field as %B.  We use double percent sign (%%)  in %%B to indicate that this a variable and not an input parameter.

Fantastic!  Now variable DFMT contains string “M/d/yyyy”.  Based on this variable I can further direct my script to extract year from a proper position from an output of a DATE command.

Note:  you have to place this command in *.BAT or *.CMD file.  You cannot run this from a command prompt.

Tags: , , , ,

4 Responses to Read Registry value into a batch variable

  1. MazterGee
    2017/03/17 at 21:25

    Udar Gromov , you are a Genius thank you.

    I needed to get the 3rd value of a Reg Query, the actual Data from the Value , and your code did the trick perfectly. I needed to get the ProfilePath for a specific SID which I obtained from a WMIC command.

    thanks so much for sharing your knowledge with the world

  2. Ferdinand Casserly
    2015/09/03 at 16:19

    Part of the maintenance we do at my work involves rebuilding user’s profiles and in windows 7 the registry key for the old profile needs to be deleted before the new profile will work correctly. Batch file to find files?

  3. Hi-Grage
    2013/10/08 at 10:28

    Aaron, you can’t run this command from a command prompt.
    Place both lines of code into a CMD file and then run it.
    If you do that, you get a desired result.
    If you run this CMD file (“test.cmd”), you will get output similar to example below:


    C:\>FOR /F “skip=2 tokens=2,*” %A IN (‘reg.exe query “HKCU\Control Panel\Interna
    tional” /v “sShortDate”‘) DO set “DFMT=%B”

    C:\>set “DFMT=M/d/yyyy”

    C:\>ECHO New variable DFMT = M/d/yyyy
    New variable DFMT = M/d/yyyy

  4. Aaron
    2013/10/07 at 20:33

    Tried this with error:
    FOR /F “skip=2 tokens=2,*” %%A IN (‘reg.exe query “HKCU\Control Panel\International” /v “sShortDate”‘) DO set “DFMT=%%B”

    %%A was unexpected at this time.

Add Your Comment Ваш Комментарий

Your email address will not be published. Required fields are marked *

* Ваше Имя *
* Ваш Email (не будет показан на сайте)*