Search code examples
batch-filecmdwindows-scripting

Querying a registry key in a batch script


I'm using the following code to get a list of programs being run at start up, and log them to a file.

for /f "skip=2 tokens=1,2*" %%A in ('REG QUERY "HKCU\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run" 2^>NUL') do echo %%A : %%C >> Log.txt

This works with entries where the value name doesn't contain a space, but when it does, such as with "Google Update", it messes up the tokens, and %%C becomes: REG_SZ <path>, instead of just the path.

Does anyone have a better way to query the registry and log its values?


Solution

  • Well I got one working solution, I'd still love to see if anyone has anything better.

    for /f "skip=2 tokens=*" %%A in ('REG QUERY "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" 2^>NUL') do (
        set regstr=%%A
        set regstr=!regstr:    =^|!
    
        for /f "tokens=1,3 delims=  |" %%X in ("!regstr!") do (
            echo %%X : %%Y
        )
    )