In Microsoft Windows PowerShell 3.0, the Common Information Model (CIM) exposes a new application programming interface (API) for working with Windows Management Instrumentation (WMI) information.

 All CIM classes support tab expansion of the namespace parameter, as well as the -class parameter. But to explore WMI classes, you want to use the Get-CimClass cmdlet.

The default WMI namespace on all operating systems after Windows NT 4.0 is Root/ Cimv2. The only time you need to change the default WMI namespace (via the namespace parameter) is when you need to use a WMI class from a nondefault WMI namespace.

Using the -classname parameter

Using the Get-CimClass cmdlet, you can use wildcards for the -classname parameter to enable you to quickly identify potential WMI classes for perusal. You can also use wildcards for the -qualifiername parameter.

Get-CimClass -ClassName *computer*

Note If you try to use a wildcard for the -classname parameter of the Get-CimInstance cmdlet, an error returns because the parameter design does not permit wildcard characters.

Finding WMI class methods

use the -methodname to get the method you want.

If you want to  find WMI classes related to processes that contain a method that begins with the let- ters term*, you use a command similar to the one appearing here:

PS C:\> Get-CimClass -ClassName *process* -MethodName term*
Filtering classes by qualifier

To  find WMI classes that possess a particular WMI qualifier, use the -QualifierName parameter.

Deprecated:

One qualifier that is important to review is the deprecated qualifier. Deprecated WMI classes are not recommended for use because they are being phased out. Using the Get-CimClass cmdlet makes it easy to spot these WMI classes. This technique appears here:

PS C:\> Get-CimClass * -QualifierName deprecated

Using this technique, it is easy to  find association classes. Association classes relate two different WMI classes. For example, the Win32_DisplayCon guration WMI class relates displays and the associated con guration. The code that follows  finds all of the WMI classes in the root/cimv2 WMI namespace that relate to sessions. In addition, it looks for the association qualifier. Luckily, you can use wildcards for the qualifier names; in keeping with this, the following code uses assoc* instead of the typed-out association.

PS C:\> Get-CimClass -ClassName *session* -QualifierName assoc*

Dynamic:

One qualifier you should definitely look for is the dynamic qualifier. This is because querying abstract WMI classes is unsupported. An abstract WMI class is basically a template class that is used by WMI when creating new WMI classes. Therefore, all dynamic WMI classes will derive from an abstract class. Therefore, when looking for WMI classes, you will want to ensure that at some point you run your list through the dynamic filter.

By adding the query for the qualifier, you identify the appropriate WMI classes. One class is abstract, and the other two are dynamic classes that could prove to be useful. In the following code, the dynamic qualifier is first used, and the abstract qualifier appears second.

PS C:\> Get-CimClass -ClassName *time -QualifierName dynamic
NameSpace: ROOT/cimv2
CimClassName CimClassMethods CimClassProperties
------------ --------------- ------------------
Win32_LocalTime {} {Day, DayOfWeek, Hour, Millis...
Win32_UTCTime {} {Day, DayOfWeek, Hour, Millis...
PS C:\> Get-CimClass -ClassName *time -QualifierName abstract
NameSpace: ROOT/cimv2
CimClassName CimClassMethods CimClassProperties
------------ --------------- ------------------
Win32_CurrentTime {} {Day, DayOfWeek, Hour, Millis...
Retrieving WMI instance

The Get-CimInstance or gcim cmdlet returns the entire WMI object, but it honors the *.format.ps1xml files that Windows PowerShell uses to determine which properties are displayed by default for a particular WMI class. The command appearing here shows the properties available from the

Win32_Bios WMI class:

PS C:\> $b = Get-CimInstance win32_bios
PS C:\> $b.CimClass.CimClassProperties | fw name -Column 3
Caption Description InstallDate
Name Status BuildNumber
CodeSet IdentificationCode LanguageEdition
Manufacturer OtherTargetOS SerialNumber
SoftwareElementID SoftwareElementState TargetOperatingSystem
Version PrimaryBIOS BiosCharacteristics
BIOSVersion CurrentLanguage InstallableLanguages
ListOfLanguages ReleaseDate SMBIOSBIOSVersion
SMBIOSMajorVersion SMBIOSMinorVersion SMBIOSPresent
 Reducing returned properties and instances

you can reduce the number of properties returned, as well as the number of instances. To reduce properties, use the -property parameter.
To reduce the number of returned instances, use the -filter parameter.

PS C:\> gcim -clas win32_service -Property name, state -Filter "name = 'bits'"
Name : BITS
Status :
ExitCode :
DesktopInteract :
ErrorControl :
PathName :
ServiceType :
StartMode :
Caption :
Description :
InstallDate :
CreationClassName :
Started :
SystemCreationClassName :
SystemName :
AcceptPause :
AcceptStop :
DisplayName :
ServiceSpecificExitCode :
StartName :
State : Running
TagId :
CheckPoint :
ProcessId :
WaitHint :

To produce cleaner output, send the selected data to the Format-Table cmdlet (you can use the ft alias for the Format-Table cmdlet to reduce typing).

PS C:\> gcim -clas win32_service -Property name, state -Fil "name = 'bits'" | ft name, state
name     state
----     -----
BITS     Running

Make sure you choose properties you have already selected in the -property parameter, or else they will not display. In the command appearing here, the status property is selected in the Format-Table cmdlet. There is a status property on the WIN32_Service WMI class, but it was not chosen when the properties were selected.

PS C:\> gcim -clas win32_service -Property name, state -Fil "name = 'bits'" |
ft name, state, status
name    state    status
----    -----    ------
BITS    Running

To prevent this mistake happening, one thing that can simplify some of your coding is to put your property selection into a variable. This permits you to use the same property names in both the Get-CimInstance cmdlet and the Format-Table cmdlet (or Format-List or Select-Object, or
whatever you are using after you get your WMI data) without having to type things twice. This technique appears here:

PS C:\> $property = "name","state","startmode","startname"
PS C:\> gcim -clas win32_service -Pro $property -fil "name = 'bits'" | ft $property -A
name     state     startmode     startname
----     -----     ---------     ---------
BITS     Running   Manual        LocalSystem

 

Work with Associations