Creating WMI Clients

WMI provides a standardized system management infrastructure that can be leveraged by a number of different clients. These clients range from the wmic.exe command line tool to System Center Operations Manager. You can write your own WMI clients by using either the WMI Scripting API, the native C++ API or by using the types in the System.Management .NET Framework class library namespace.

How to create a WMI client

The core functionality of WMI consists of retrieving objects from the WMI repository and examining the properties of those objects. You can also choose to update those properties, or call methods on those properties. The following examples show how to perform a basic WMI administration task: retrieving the name of the local computer.

Term Description
Creating a client with PowerShell
WMI and PowerShell are tightly integrated; as such, retrieving WMI objects with PowerShell is simply a matter of calling the Get-WmiObject cmdlet. Note that for consistency, the first code snippet explicitly states many of the default values; the second assumes that the default values are correct.

PowerShell
#explicitly states many of the default parameters
$myComputer = Get-WmiObject -ComputerName "." -Namespace "root\cimv2" -Query "SELECT * FROM Win32_ComputerSystem"
foreach ($computer in $myComputer)
{ "System Name: " + $computer.name }

#assumes the default values are correct Get-WmiObject Win32_ComputerSystem | Format-Table "Name"

Creating a client with VBScript

VBScript was the original scripting language that had common use with WMI. While PowerShell has become more popular, many of the existing code samples in this documentation are written in VBScript. Note that this particular VBScript sample explicitly states both the local machine path as well as the impersonation level; this is not required, but is often a best practice.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objItem in colItems
    Wscript.Echo "Computer Name: " & objItem.Name
Next

Creating a client with C# (Microsoft.Management.Infrastructure)

This namespace contains the current solution for accessing WMI with managed code, and is known as the Windows Management Infrastructure (MI, or WMIv2). Currently, MI is the supported technology for creating managed management clients. For more information, see How to Implement a Managed MI Client and How to Implement a Native MI Client.

C#
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem");

foreach (CimInstance cimObj in queryInstance) { Console.WriteLine(cimObj.CimInstanceProperties["Name"].ToString()); }

Creating a client with C# (System.Management)

This namespace contains the original solution for accessing WMI with managed code. While the System.Management classes are still available, the Microsoft.Management.Infrastructure classes are generally more efficient and scale better. As such, it is recommended that you use the MI classes, rather than the original WMI classes.

C#
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem");

foreach (CimInstance cimObj in queryInstance) { Console.WriteLine(cimObj.CimInstanceProperties["Name"].ToString()); }

The following table lists the topics covered in this section.

Topic Description
Connecting to WMI on a Remote Computer Describes a number of issues that arise when clients use the WMI infrastructure on a remote computer.
WMI Tasks for Scripts and Applications Shows example WMI client code.
Creating a WMI Application or Script Provides information about creating various WMI clients.
Monitoring Performance Data Describes how to use WMI to monitor performance data.
Receiving a WMI Event Describes how to view WMI events.
Monitoring Events Describes how to monitor WMI events.
Querying with WQL Introduces the WMI Query Language (WQL).
Querying the Status of Optional Features In Windows 7, WMI implemented the Win32_OptionalFeature class. This class retrieves the status of the optional features that are present on a computer.
Describing the Location of a WMI Object Focuses on the syntax for describing the location of a WMI managed entity.
Accessing Other Operating System Features with WMI Describes how to write WMI clients that access device drivers, Active Directory, and SNMP devices.
Accessing Data in the Interop Namespace Association providers enable Windows Management Instrumentation (WMI) clients to traverse and retrieve profiles and associated class instances from different namespaces.
Manipulating Class and Instance Information Describes the common tasks that WMI clients must perform.
Linking Classes Together Discusses the view provider and how it can be used to bring together information from multiple WMI classes.
Modifying the System Registry Describes how WMI clients can use WMI to manage system registry information.