This post originated from an RSS feed registered with .NET Buzz
by Scott Hanselman.
Original Post: BATCH FILE VOODOO: Determine if multiple (and which) versions of an MSI-installed Product are...
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
We have a product that supports side-by-side installs, and I wanted to enumerate all
the versions of that product and display it's version/product name. Sure, it
could be done in yet-another-C# program, but why not use Batch?
Every MSI installer has an UpgradeCode GUID that can't change for
the life of the product. The ProductCode and PackageCode may change, but the
UpgradeCode is what tells us if the product is already installed. At that point
the installer can decide if it wants to upgrade or just install along side.
Those UpgradeCodes (for all apps) are in HKCU\Software\Microsoft\Installer\UpgradeCodes\ with
SubKeys for each Product while the Products are in HKCU\Software\Microsoft\Installer\Products\ with
details for each like Version, ProductName, etc. In this example I'm getting
the ProductName which includes the version for me, but of course you can
get anything you want.>
NOTE: We're using REG.EXE which
is included in the PATH on 2003 and XP but is an add-on to Windows 2000.
Here's the voodoo (all one line). Note - The delims= includes a TAB, then a
space:
FOR
/F "tokens=1 skip=4 delims=
" %%A IN ('REG QUERY HKCU\Software\Microsoft\Installer\UpgradeCodes\<yourUpgradeCodeGUIDHere>
/s') DO FOR /F "tokens=5 delims=
" %%Z IN ('REG QUERY HKCU\Software\Microsoft\Installer\Products\%%A /v ProductName')
DO ECHO %%Z
Here it is broken down:
FOR /F
"tokens=1 skip=4 delims=
" %%A
IN (
'REG QUERY HKCU\Software\Microsoft\Installer\UpgradeCodes\<yourUpgradeCodeGUIDHere>
/s'
)
DO
FOR /F "tokens=5 delims=
" %%Z
IN (
'REG QUERY HKCU\Software\Microsoft\Installer\Products\%%A
/v ProductName'
)
DO
ECHO %%Z