Bewerken

Delen via


WMI Tasks: Computer Software

WMI tasks for computer software obtain information such as which software is installed by the Microsoft Windows Installer (MSI) and software versions. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

To run a script

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

Note

Running a "Select * from Win32_Product" query may result in unexpected behavior. This is because the provider that supports Win32_Product is not query optimized. For more information, see KB Article 974524.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...uninstall software using a script? If the software was installed using Microsoft Windows Installer (MSI), use the WMI class Win32_Product and the Uninstall method.
VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
    ("Select * from Win32_Product " _
        & "Where Name = 'Personnel database'")
For Each objSoftware in colSoftware
    objSoftware.Uninstall()
Next

PowerShell
$colSoftware = Get-WmiObject -Class Win32_Product | Where-Object {$_.name -eq "Personnel database"}

foreach ($colItem in $colSoftware) { $colItem.Uninstall() }

...inventory all the software installed on a computer with a script?

If the software was installed using Microsoft Windows Installer (MSI) use the WMI class Win32_Product.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
    ("Select * from Win32_Product")

For Each objSoftware in colSoftware Wscript.Echo "Name: " & objSoftware.Name Wscript.Echo "Version: " & objSoftware.Version Next

PowerShell
$colSoftware = Get-WmiObject -Class Win32_Product

foreach ($colItem in $colSoftware) { "Name: " + $colItem.Name "Version: "+ $colItem.Version }

...determine what version of Microsoft Office is installed?

Use the Win32_Product class and check the value of the Version property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery(_
    "Select * from Win32_Product " & _
    "Where IdentifyingNumber =" _
        & " '{90280409-6000-11D3-8CFE-0050048383C9}'")
For Each objItem in colSoftware
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "Version: " & objItem.Version
Next

PowerShell
$colSoftware = Get-WmiObject -Class Win32_Product | Where-Object {$_.IdentifyingNumber -eq "{90280409-6000-11D3-8CFE-0050048383C9}"}

foreach ($colItem in $colSoftware) { "Name: " + $colItem.Name "Version: " + $colItem.Version }

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter