Query Registry for .msi file names associated to programs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DoingDOS
Posts: 6
Joined: 29 Aug 2013 20:06

Query Registry for .msi file names associated to programs

#1 Post by DoingDOS » 30 Aug 2013 13:31

I'm looking for some ideas and assistance on how best to approach this using batch.

If possible I want to be able to do a couple of methods for listing what the .MSI files in the C:\Windows\Installer folder are for.

I can use WSH (Windows Scripting Host) to create an object and then read the information but then you're tied to WSH which I'd rather not do.

Set objInstaller = CreateObject("WindowsInstaller.Installer")
objInstaller.SummaryInformation.Property(3)


Querying the Registry will actually provide you with that information and is stored in this location

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products


As an example this key stores the information for "Microsoft Silverlight"

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\D7314F9862C648A4DB8BE2A5B47BE100\InstallProperties
DisplayName = the product name
LocalPackage = path and file name for the .msi file associated to the program installation for reinstall or repair build.

In this case as an example it is listed for my computer as: c:\Windows\Installer\5da99.msi but is a random name for all computers.

So doing a direct listing I can get it to return that it found it. But at this point I'm not sure how to return the full registry path where it found it so that I can query the other 2 values in that location.

Code: Select all

for /f "tokens=2,*" %%a in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products" /f "Microsoft Silverlight" /s /e /d ') DO echo %%b


What I think I need is to somehow store the base key and the final key as variables in order to more effeciently use them.

BaseKey
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\

ProductKey
D7314F9862C648A4DB8BE2A5B47BE100

PropertiesKey
InstallProperties

BaseKey and PropertiesKey always remain the same but the ProductKey is unknown or different on all computers depending on what software is installed.

My objective is to either feed a specific .msi file name to the batch to find out what it's DisplayName is but preferred is to simply query all the subkeys and then print out to file the DisplayName and Location for all the .msi files.

Or if possible provide the batch file with the name of the product to look for and then have it find that .msi file

Example output
Microsoft Silverlight: c:\Windows\Installer\5da99.msi
Adobe® Content Viewer: C:\Windows\Installer\29024037.msi
MSXML 4.0 SP2 (KB954430): C:\Windows\Installer\26e09f2.msi


Would greatly appreciate the time and effort involved if someone can assist me in how to achieve this.

Thank you

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Query Registry for .msi file names associated to program

#2 Post by foxidrive » 31 Aug 2013 01:42

Does the registry identify which files are .msi that have been installed?

How do you tell which registry key is associated with a .MSI install?

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Query Registry for .msi file names associated to program

#3 Post by aGerman » 31 Aug 2013 09:12

The search function of reg.exe is buggy. I'd suggest to go a different way:

Code: Select all

@echo off &setlocal
set "Product=Adobe Reader"

for /f "delims=" %%i in (
  'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"^|findstr /b "HKEY_LOCAL_MACHINE\\"'
) do (
  reg query "%%i\InstallProperties" /v "DisplayName"|findstr /ic:"%Product%" >nul && (
    set "RegPath=%%i"
    set "ProductKey=%%~nxi"
  )
)
echo Product Key: %ProductKey%

for /f "tokens=2*" %%i in (
  'reg query "%RegPath%\InstallProperties" /v "DisplayName"'
) do set "DisplayName=%%j"
echo Display Name: %DisplayName%

for /f "tokens=2*" %%i in (
  'reg query "%RegPath%\InstallProperties" /v "LocalPackage"'
) do set "MSIPath=%%j"
echo MSI Path: %MSIPath%

pause

Regards
aGerman

DoingDOS
Posts: 6
Joined: 29 Aug 2013 20:06

Re: Query Registry for .msi file names associated to program

#4 Post by DoingDOS » 03 Sep 2013 13:18

@foxidrive

Yes that information is in the Registry.

Thanks aGerman

That is very close and does work but has a couple of errors so I'll try to track those down and post back what I find.

Thank you again

DoingDOS
Posts: 6
Joined: 29 Aug 2013 20:06

Re: Query Registry for .msi file names associated to program

#5 Post by DoingDOS » 03 Sep 2013 15:06

Have not had time to really dig into the code yet, but here is what was causing the errors so I'll probably need to add some error checking code.


This key has no InstallProperties sub-key and why it gets an error.
Appears to be related to Microsoft Security Essentials and Out of Box Experience
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\2303D87CDFD90D14D99E85AE7E05BC4A

This key has no DisplayName entry and why it gets an error
Appears to be related to the Intel Gigabit V16500 network card drivers
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\90B53772EFE9F9143A7701AA18113CA0\InstallProperties

This key has no DisplayName entry and why it gets an error
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\F7322F45C810B3848848F90C8D88043C\InstallProperties
Appears to be related to the Intel VC_CRT_x64 runtime

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Query Registry for .msi file names associated to program

#6 Post by aGerman » 03 Sep 2013 16:53

In that case you could suppress the error messages by redirecting them to NUL.
Untested:

Code: Select all

@echo off &setlocal
set "Product=Adobe Reader"

for /f "delims=" %%i in (
  'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"^|findstr /b "HKEY_LOCAL_MACHINE\\"'
) do (
  reg query "%%i\InstallProperties" /v "DisplayName" 2>nul|findstr /ic:"%Product%" >nul && (
    set "RegPath=%%i"
    set "ProductKey=%%~nxi"
  )
)
echo Product Key: %ProductKey%

for /f "tokens=2*" %%i in (
  'reg query "%RegPath%\InstallProperties" /v "DisplayName" 2^>nul'
) do set "DisplayName=%%j"
echo Display Name: %DisplayName%

for /f "tokens=2*" %%i in (
  'reg query "%RegPath%\InstallProperties" /v "LocalPackage"  2^>nul'
) do set "MSIPath=%%j"
echo MSI Path: %MSIPath%

pause

Regards
aGerman

DoingDOS
Posts: 6
Joined: 29 Aug 2013 20:06

Re: Query Registry for .msi file names associated to program

#7 Post by DoingDOS » 05 Sep 2013 22:19

Sorry I've not had time to dig into this further as I've been doing 18+ hours work on some unexpected emergency work.
Didn't want to leave you hanging - waiting for a reply as I do really appreciate it and will be back soon to study your code and if I can't find the answers here I'll post some questions about your code to better understand it so that I can modify it.

Initially just running the code does appear to work. But I need to do a strict search and not a fuzzy search as the current code finds and stops at the first Adobe it finds but on a system with dozens of Adobe products it won't find Reader or any other specific product.

I need to read up more to better understand.

Thank you again aGerman

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Query Registry for .msi file names associated to program

#8 Post by aGerman » 06 Sep 2013 13:11

DoingDOS wrote:But I need to do a strict search and not a fuzzy search as the current code finds and stops at the first Adobe it finds but on a system with dozens of Adobe products it won't find Reader or any other specific product.

The /c: option of FINDSTR makes it search for the entire string in %Product% regardless whether or not spaces are present in the string. Thus, in my example it is looking for "Adobe Reader" (case insensitive because of the /i option).
Actually I thougt that would be sufficient to find the right key and my intention was to keep the code flexible enough to find the Adobe Reader key without knowing the currently installed version. Of course you could change the options /ic: to /ec: to make it case sensitive and to make sure the string was found at the end of the line. I think trying to make it act much more strictly would be a waste of effort :wink:

Regards
aGerman

DoingDOS
Posts: 6
Joined: 29 Aug 2013 20:06

Re: Query Registry for .msi file names associated to program

#9 Post by DoingDOS » 06 Sep 2013 16:13

I'll play with the switches but in basic testing the other day I did not find that it altered the output.

I need it to be strict because it will be used as a routine in a larger batch script so that it can find a specific .msi file and actually delete it.
As it is now it simply finds what I think is the last entry for Adobe in the loop so without being strict I will not be able to determine what .msi file to delete.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Query Registry for .msi file names associated to program

#10 Post by aGerman » 06 Sep 2013 16:39

Please try out the above mentioned modification in line 7 of my example

Code: Select all

  reg query "%%i\InstallProperties" /v "DisplayName" 2>nul|findstr /ec:"%Product%" >nul && (

I'm pretty sure you will always find the right key.
Adobe Reader
shouldn't work anymore. You'll need the whole string (
Adobe Reader XI (11.0.03) - Deutsch
in my case).

Regards
aGerman

DoingDOS
Posts: 6
Joined: 29 Aug 2013 20:06

Re: Query Registry for .msi file names associated to program

#11 Post by DoingDOS » 06 Sep 2013 18:15

You're too kind aGerman - yes it does work.

I will still need to read up so that I can learn and understand all the code used here.

Thank you again for your kind help and support with this, very much appreciated.

Post Reply