开发者

Query the Windows registry through batch syntax

I'm trying to query a particular registry folder (or whatever you want to call it) to obtain some information.

Particularly the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall folder contains a list of installed software.

The issue is that each software is identified through a random key value like {0001B4FD-9EA3-4D90-A79E-FD14BA3AB01D} instead of the actual software (like Skype).

This makes it hard to find the Skype identifier because I need to loop through everything inside this Uninstall folder and check whether the DisplayName value corresponds to Skype (or whatever other application name).

I need to use batch syntax... this is what I have so far but it doesn't behave the same on different computers, maybe I get different variables assigned based on some erroneous formatting of the reg output? I don't know. Can I use a data structure to contain whatever reg outputs? Anything would work.

@echo off
for /f "tokens=*" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall') do (
    for /f "tokens=2,* delims= " %%b in ('reg q开发者_StackOverflowuery %%a /v Publisher') do (
        IF "%%c" == "Skype Technologies S.A." (
            for /f "tokens=2,* delims= " %%d in ('reg query %%a /v UninstallString') do (
                echo %%e
            )
        )
    )

)

Is there a cleaner and safer way to achieve this in batch?


It seems me more easy to use

reg QUERY HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall  /f "Skype Technologies S.A." /s

as a basis for your batch file. It produces a simple output like following

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{D103C4BA-F905-437A-8049-DB24763BBE36}
    Publisher    REG_SZ    Skype Technologies S.A.

End of search: 1 match(es) found.


This batch loop through items under the Uninstall key and search for the defined software name e.g. Skype. Once it located the software and the full key, it will then use it to search and print out DisplayName and UninstallString as shown in Output below.

Note:

  1. For some software e.g. Skype, Notepad++ the key may not contain a GUID e.g. {BEB5FB69-4080-466F-96C4-F15DF271718B}. This batch is able to find software with or without a GUID
  2. It can return multiple software if the name is too short
  3. Use %x86GUID% variable to search 32 bit software and %x64GUID% for 64 bit software

.

@echo off
setlocal ENABLEDELAYEDEXPANSION

set SoftwareName=Skype
set x86GUID=HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
set x64GUID=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

REM It's faster to first locate the software GUID, then search it's Name, Version & UninstallString
for /f "delims=" %%P in ('reg query "%x86GUID%" /s /f "%SoftwareName%" 2^>nul ^| findstr "Uninstall\\%SoftwareName% Uninstall\\{"') do (
  echo %%P
  for /f "tokens=2*" %%A in ('reg query "%%P" /v "DisplayName" 2^>nul ^|findstr "DisplayName"') do echo Found: %%B
  for /f "tokens=2*" %%A in ('reg query "%%P" /v "UninstallString" 2^>nul ^|findstr "UninstallString"') do echo %%B 
)
endlocal

Output

Key: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Skype_is1
DisplayName: Skype version 8.64
UninstallString: "C:\Program Files (x86)\Microsoft\Skype for Desktop\unins000.exe"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜