tags:

views:

3187

answers:

3

I have a script which finds specific installed software but I am having trouble also getting the software's version. For example, say I am getting a list of all Microsoft software installed. Here is what I have thus far:

echo software installed > software_list.txt
echo ================= >>software_list.txt
reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall temp1.txt
find "Microsoft" temp1.txt| find "DisplayName" > temp2.txt
for /f "tokens=2,3 delims==" %%a in (temp2.txt) do (echo %%a >> software_list.txt)

start notepad "software_list.txt"

del temp1.txt temp2.txt

How can I also get the DisplayVersion from the reg export? If I replace DisplayName with DisplayVersion, nothing is even found. Or, is there another avenue I should be taking here?

+1  A: 

[Shamelessly copy/pasted @Helen's answer starts here]

If the software you're interested in is installed by the Windows Installer, you can get info about that software (such as the name, vendor, version etc) by querying the WMI Win32_Product class. In batch files, this can be done using the WMI command-line utility wmic. Here're some examples:

*

  Print the names and versions of installed software:

  wmic product get Name, Version

*

  List all installed Microsoft products:

  wmic product where "Vendor like '%Microsoft%'" get Name, Version

*

  List installed products that have Office in their names:

  wmic product where "Name like '%Office%'" get Name, Version

To save the wmic output to a file, you can use the /output and/or /format parameters, e.g.:

wmic /output:software.txt product get Name, Version wmic /output:software.htm product get Name, Version /format:htable

For more information about the wmic syntax, see wmic /?

[End of shamelessly copy/pasted answer from @Helen ends here.]

If the software wasnt' installed by Windows Installer, rather than look in the registry, you could look in the exes themselves. You need something beyond a mere .bat file. You need something that can open the exes and extract the version info.

I would take a look at PowerShell, which is windows successor to .bat files. Use System.Diagnostics.FileVersionInfo.GetVersionInfo to get the version.

Corey Trager
A: 

Another possibility beyond PowerShell (which is a good one) is to use WMI with JScript or VBScript to access the software store.

phoebus
+3  A: 

Replacing DisplayName with DisplayVersion results in an empty output because of the way this line works:

find "Microsoft" temp1.txt| find "DisplayName" > temp2.txt

What this line does is it finds all lines in the temp2.txt file that contain both the Microsoft and DisplayName substrings (that is, it finds the products whose names contain Microsoft). The lines with DisplayVersion, in their turn, contain product version numbers and don't contain the word Microsoft, which is why you get empty output.

I can suggest a couple of alternative solutions that use WMI:

  1. Parse the HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall subkeys using a script (VBScript, PowerShell etc) rather than a batch file, because scripting languages offer much better support for text manipulation. Here's a VBScript example that outputs the names and versions of installed Microsoft products (products whose names contain Microsoft, to be more precise):

    On Error Resume Next
    
    
    Const strComputer = "."
    Const HKLM        = &H80000002
    Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    
    
    Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion
    
    
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
         strComputer & "\root\default:StdRegProv")
    
    
    ' Enumerate the subkeys of the Uninstall key
    oReg.EnumKey HKLM, strKeyPath, arrSubKeys
    For Each strProduct In arrSubKeys
      ' Get the product's display name
      oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
      ' Process only products whose name contain 'Microsoft'
      If InStr(1, strDisplayName, "Microsoft", vbTextCompare) > 0 Then
        ' Get the product's display version
        oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
        WScript.Echo strDisplayName & vbTab & strVersion
      End If
    Next
    

    Usage:

    cscript //nologo productlist.vbs
    cscript //nologo productlist.vbs > productlist.txt
  2. If the software you're interested in is installed by the Windows Installer, you can get info about that software (such as the name, vendor, version etc) by querying the WMI Win32_Product class. The wmic utility lets you do this directly from the command line and batch files. Here're some examples:

    • Print the names and versions of installed software:

      wmic product get Name, Version
      
    • List all installed Microsoft products:

      wmic product where "Vendor like '%Microsoft%'" get Name, Version
      
    • List installed products that have Office in their names:

      wmic product where "Name like '%Office%'" get Name, Version
      

    To save the wmic output to a file, you can use the /output and (optionally) /format parameters, e.g.:

    wmic /output:software.txt product get Name, Version
    wmic /output:software.htm product get Name, Version /format:htable
    

    For more information about the wmic syntax, see wmic /?

Helen