• Remotely check if registry key exists

    Home » Forums » AskWoody support » Windows » Windows – other » Remotely check if registry key exists

    Author
    Topic
    #437319

    Edited by HansV to replace % with [%] tags, since % followed by a hexadecimal number messes up the display.

    I’m trying to use a BATch file to find if a registry key exists, and populate two lists from there. Here’s what I have so far, as well as the proposed usage using a command line utility called PsExec:

    BATch File
    —————————————–
    @echo off
    if exist “HKLMSOFTWAREGoogleGoogle Toolbar” GOTO GOOD
    if not exist “HKLMSOFTWAREGoogleGoogle Toolbar” GOTO BAD
    :GOOD
    echo %computername% >> usmc_nfstranspoupdatesgoogletoobar.txt
    GOTO END
    :BAD
    echo %computername% >> usmc_nfstranspoupdatesnogoogletoolbar.txt
    GOTO END
    :END
    ——————————————

    I know I’m just missing something small, but I don’t remember what.

    Usage:

    c:> psexec @c:computers.txt -u admin -c usmc_nfsftranspoupdatesgoogletoolbar.bat

    shrug

    Viewing 1 reply thread
    Author
    Replies
    • #1039285

      Consider this thread

      • #1039287

        I can’t afford a membership to that site to see the answer groan

        • #1039289

          Just scroll down to see the answer, and don’t let the site set cookies….

    • #1039310

      IF EXIST determines whether or not a FILE exists. It has nothing whatever to do with registry keys!

      You need REG QUERY “registry key/value” (from a sufficiently-privileged account) and examine what is returned.

      e.g.

      @echo off
      :: my PC is named DELLIGHTFUL.  Honest.
      reg query "dellightfulhklmsoftwaregooglegoogle toolbar"
      echo errorlevel was %errorlevel%
      reg query "dellightfulhklmsoftwaregigglegiggle toolbar"
      echo errorlevel was %errorlevel%

      gives

      ! REG.EXE VERSION 3.0
      
      HKEY_LOCAL_MACHINEsoftwaregooglegoogle toolbar
          test	REG_SZ	41
      
      HKEY_LOCAL_MACHINEsoftwaregooglegoogle toolbar4.0
      
      HKEY_LOCAL_MACHINEsoftwaregooglegoogle toolbarBranding
      
      HKEY_LOCAL_MACHINEsoftwaregooglegoogle toolbarInstallations
      
      HKEY_LOCAL_MACHINEsoftwaregooglegoogle toolbarObsolete
      errorlevel was 0
      
      Error:  The system was unable to find the specified registry key or value
      errorlevel was 1

      So do the
      REG QUERY xxx 1>nul 2>&1
      to suppress all the output, good or bad, and test the resulting errorlevel.

      John

      • #1039313

        I’ve even done it for you…

        @echo off
        :: we assume that computer names are found one per line...
        for /f %%a in (c:computers.txt) do call :process %%a
        goto :eof
        
        ::--------------------------------------------------
        :process    the computername passed from the FOR command
        
        reg query "%1hklmsoftwaregooglegoogle toolbar" 1>nul 2>&1
        if errorlevel 1 (
          ECHO GOOGLE TOOLBAR NOT FOUND ON %1
          REM  echo %1 >> usmc_nfstranspoupdatesnogoogletoolbar.txt
          ) else (
          ECHO GOOGLE TOOLBAR **FOUND** ON %1
          REM  echo %1 >> usmc_nfstranspoupdatesgoogletoobar.txt
          )
        goto :eof

        After testing it “as is” on your machine, presumably in your Administrator account, remove the word REM from the two lines where found, and the entire lines starting with ECHO in capital letters. You don’t always have to use PSEXEC!

        John

        PS I’ve not changed the TYPO in the filename “googletoobar.txt”…!!

    Viewing 1 reply thread
    Reply To: Reply #1039313 in Remotely check if registry key exists

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel