I want to list a computer's installed applications. All the sample scripts I've seen list only those applications installed with Windows Installer. At a minimum, I'd like to capture all the applications listed in the Control Panel Add or Remove Programs applet. How can I create such a list?
The sample scripts you reference probably use the WMI Win32_Product class, which is part of Windows Management Instrumentation's (WMI's) Windows Installer Provider. This class returns information about only those applications installed with Windows Installer (i.e., .msi files). Fortunately, many other ways exist to build a more complete list, depending on your needs and the types of applications you want to inventory. Let's look at how to list the applications in the Add or Remove Programs applet.
The registry stores information about all the applications that appear in the Add or Remove Programs applet in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall key. Beneath this key are subkeys, each of which represents an application in the Add or Remove Programs list. Beneath each subkey is a set of values that describe the application (e.g., DisplayName, DisplayVersion, InstallLocation). You can use the WMI Standard Registry Provider to enumerate the applications, as the script ListApps.vbs in Listing 2 shows. ListApps.vbs creates a reference to the WMI Standard Registry Provider class named StdRegProv, uses the reference to enumerate the subkeys under Uninstall, and echoes each application's DisplayName, DisplayVersion, and InstallLocation registry value.
Let's take a closer look at ListApps.vbs. After declaring Option Explicit, ListApps.vbs explicitly declares all the variables that it uses. Those variables include strComputer and strKey, which will hold the target computer's name and registry subkey, respectively. The script also defines the HKEY_LOCAL_MACHINE constant, which specifies the registry hive with which to connect.
Callout A in Listing 2 highlights where the real work starts. This code creates a reference (i.e., objRegistry) to the WMI Standard Registry Provider. As the WMI connection string passed to the GetObject function shows, the registry provider's class name is StdRegProv, which resides in the root\default namespace. After you obtain the reference to the registry provider, you can use the reference to call the registry provider's EnumKey and GetStringValue methods.
The code at callout B calls the registry provider's EnumKey method to obtain the list of subkeys under strKey. EnumKey takes three parameters:
- A numeric value that represents the target registry hive. In this case, the HKEY_LOCAL_MACHINE constant represents the hexadecimal value &H80000002, which maps to HKEY_LOCAL_MACHINE.
- A string (strKey) that contains the path to the registry key to enumerate.
- A variant array (arrSubKeys), which the EnumKey method populates with the subkeys under the path that the second parameter points to. This type of parameter is often called an out parameter because it represents the mechanism that the method uses to return data to the calling program.
After EnumKey executes, the arrSubKeys array contains the list of subkeys under strKey. Each subkey in the array represents an application. The code at callout C enumerates these subkeys. The VBScript error handler, On Error Resume Next, suppresses any errors that will most certainly occur if an application failed to set any of the targeted registry values. During each iteration of the For Each...Next statement, the GetStringValue method retrieves the application's DisplayName, DisplayVersion, and InstallLocation values. GetStringValue takes four parameters:
- A numeric value representing the target registry hive.
- A string that contains the path to the registry subkey that contains the value. Notice that the script concatenates strKey and strSubKey to construct the path for each application.
- The value name.
- A string to hold the returned value (i.e., another out parameter).
Following the three GetStringValue calls, the script echoes the results. Keep in mind that DisplayVersion or InstallLocation will be blank if the corresponding registry values are empty. After echoing the results, the script clears the variables before moving on to the next application. Web Figure 1 (http://www.winscriptingsolutions.com, InstantDoc ID 39314) contains a brief excerpt from ListApps.vbs's output.
To run ListApps.vbs on your local computer, use the following command:
C:\Scripts> cscript ListApps.vbs
To run ListApps.vbs against a remote WMI-enabled computer, simply change the value assigned to the strComputer variable. For example, the following change would retrieve application information from the computer named Kashmir:
strComputer = "kashmir"
To learn more about the WMI Standard Registry Provider, see the Registry chapter in the online Microsoft Windows 2000 Scripting Guide (http://www.microsoft.com/technet/scriptcenter/scrguide/sas_reg_overview.asp). For more sample scripts that use the WMI Standard Registry Provider, check out the Registry section in the Microsoft TechNet Script Center (http://www.microsoft.com/technet/scriptcenter/registry/default.asp).