Question

I have the below which is meant to do the following:

Work out all the SIDs in HKEY_USERS and then use that variable in reg query to check for the existance of a key for each HKEY_USER. However, it is telling me it is an invalid key because it basically just misses out the %%~na when it sets hkeyuserpath and then fails on the reg query. What am I doing wrong?

for /f %%a in ('reg query HKEY_USERS') do (

    echo %%~na
    set hkeyuserpath="HKEY_USERS\%%~na\Software\Microsoft\Windows\CurrentVersion\Run"

    reg query %hkeyuserpath% /v *WhatIamLookingfor*

    if "%ERRORLEVEL%" EQU "0" goto HELLO
    if "%ERRORLEVEL%" EQU "1" goto GOODBYE

    :HELLO
    echo Hello
    GOTO END

    :GOODBYE
    GOTO END
)
:END
pause
Was it helpful?

Solution

You're setting hkeyuserpath inside of a for loop, so you have to use delayedexpansion to access the variable.

setlocal enabledelayedexpansion
for /f %%a in ('reg query HKEY_USERS') do (

    echo %%~na
    set hkeyuserpath="HKEY_USERS\%%~na\Software\Microsoft\Windows\CurrentVersion\Run"

    reg query "!hkeyuserpath!" /v *WhatIamLookingFor*

    if not errorlevel 1 (
      Echo(Hello & goto :end
    ) ELSE (
      Echo(Goodbye
    )

)
:end
pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top