Cadzow Knowledgebase


Welcome
Contact Us
Professional
Services

Consulting
Knowledgebase/
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
    C:\Users\User\Documents

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

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

outputs:


    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

    HKEY_CURRENT_USER\Console
        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"

outputs:

    C:\Users\User\Music

(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:

    :RegQuery
    for /f "usebackq tokens=*" %%x in (`PowerShell "$value='%~3';(Get-ItemProperty -Path 'Registry::%~2' -Name $value).$value"`) 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%

Output:

    C:\Users\User\Documents
    C:\Users\User\Music
    1638480

Copyright © 1996-2019 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 webguru@cadzow.com.au