Search code examples
windowsbatch-fileremote-registry

Discrepancy REG QUERY local vs remote computer batch script


I'm writing a batch script to update a software package (uninstall old version/ install new one). This needs to be done over the network as there are 500 PCs to update. One of the first steps before uninstalling is checking wether that software is installed or not. In order to check that I query the registry:

reg query "HKLM\SOFTWARE\A.E.T Europe B.V."

This query gives adecuate results when running in local (for testing purposes), but when I run it remotely (they way it will be ran) returns wrong results.

reg query "\\I301\HKLM\SOFTWARE\A.E.T Europe B.V."

returns 0 if i run that line locally. But if I log into I301 and run the query locally returns 1, being the truth that A.E.T Europe B.V. shows up under the Wow6432Node branch in the windows registry.

Why is that???

Thanks in advance!


Solution

  • If there is on 64-bit Windows just the key

    HKLM\SOFTWARE\Wow6432Node\A.E.T Europe B.V.
    

    but no key

    HKLM\SOFTWARE\A.E.T Europe B.V.
    

    the reason for the different result is caused most likely by which version of reg.exe is executed from batch file or command line.

    The key is not found if 64-bit %SystemRoot%\System32\reg.exe is executed on processing the batch file or running the command by 64-bit %SystemRoot%\System32\cmd.exe on using the line

    reg query "\\I301\HKLM\SOFTWARE\A.E.T Europe B.V."
    

    But the key is found if 32-bit %SystemRoot%\SysWOW64\reg.exe is executed on processing the batch file or running the command by 32-bit %SystemRoot%\SysWOW64\cmd.exe on using the line

    reg query "HKLM\SOFTWARE\A.E.T Europe B.V."
    

    because for the 32-bit applications the registry access to HKLM\SOFTWARE is redirected to HKLM\SOFTWARE\Wow6432Node by registry redirector.

    Check both possible key locations:

    @echo off
    %SystemRoot%\System32\ping.exe -n 1 I301 >nul
    if errorlevel 1 (
        echo Computer with name I301 is not available in network.
        goto :EOF
    )
    
    %SystemRoot%\System32\reg.exe query "\\I301\HKLM\SOFTWARE\A.E.T Europe B.V." >nul 2>&1
    if not errorlevel 1 goto Installed
    %SystemRoot%\System32\reg.exe query "\\I301\HKLM\SOFTWARE\Wow6432Node\A.E.T Europe B.V." >nul 2>&1
    if not errorlevel 1 goto Installed
    
    echo A.E.T Europe B.V. is not installed.
    goto :EOF
    
    :Installed
    echo A.E.T Europe B.V. is installed already.
    

    See also the Microsoft documentation pages: