Cadzow Knowledgebase

Contact Us

Site Search

Remote Support

Print Friendly

Scripting: Fetch Registry Values for Fun and Profit

Since Microsoft Windows XP, reg.exe has been the in-box method to retrieve/set registry values. While it is relatively simple to parse values from a reg query command, it presents some challenges in longer or complex scripts where a consistent mechanism for retrieving registry values may be desirable.

For example, consider the following command:

    reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal

The output of this command is:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
        Personal    REG_SZ    C:\Users\User\Documents

Although the output contains a leading blank line, a header and a trailing blank line, this is easy to parse to extract the required value:

    for /f "usebackq tokens=1-2,*" %%x in (`reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal ^| find /i "Personal"`) do echo %%z

However, consider if the variable name being requested contains spaces:

    reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Music"


    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
        My Music    REG_SZ    C:\Users\User\Music

In this case the required value is the fourth token (1-3,*) of the output instead of the third (1-2,*). This makes the prospect of a generic approach to retrieving registry values complex.

Additionally, if the value is a REG_DWORD, reg.exe returns the hexadecimal representation of the value and for scripting purposes this usually needs converting to decimal (with set /A).

    reg query HKCU\Console /v ScreenBufferSize

        ScreenBufferSize    REG_DWORD    0x190050

PowerShell contains a method Get-ItemProperty which can solve these issues:

    powershell "$value='My Music';(Get-ItemProperty -Path 'Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders' -Name $value).$value"



(The purpose of preloading the required key name into $value is to make it easier to process the results from Get-ItemProperty if it contains spaces.)

This syntax could be formed into a subroutine within a script as follows:

    for /f "usebackq tokens=*" %%x in (`PowerShell "$value='%~3';(Get-ItemProperty -Path 'Registry::%~2' -Name $value).$value"`) do set %~1=%%x
    goto :EOF

or alternatively:

    for /f "usebackq tokens=*" %%x in (`PowerShell "(Get-Item 'Registry::%~2').GetValue('%~3')"`) do set %~1=%%x
    goto :EOF

To use the routine:

    call :RegQuery Result "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" Personal
    echo %Result%

    call :RegQuery Result "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" "My Music"
    echo %Result%

    call :RegQuery Result HKCU\Console ScreenBufferSize
    echo %Result%



Copyright © 1996-2020 Cadzow TECH Pty. Ltd. All rights reserved.
Information and prices contained in this website may change without notice. Terms of use.

Question/comment about this page? Please email