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
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());
}
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.
In Windows 7, WMI implemented the Win32_OptionalFeature class. This class retrieves the status of the optional features that are present on a computer.
Association providers enable Windows Management Instrumentation (WMI) clients to traverse and retrieve profiles and associated class instances from different namespaces.
This learning path covers Windows Management Instrumentation (WMI) and Common Information Model (CIM). These technologies help to access information about a computer. Additionally, both technologies provide local and remote access to management information from the operating system, computer hardware, and installed software.