Guide to WMI and CIM with PowerShell

WMI (Windows Management Instrumentation) exposes the state and configuration of a computer as classes organised into namespaces. CIM (Common Information Model) is the standard model WMI is built on. This guide summarises what you need to query them from PowerShell, with snippets written in the PoshCode style: full cmdlet and parameter names, no aliases, no positional parameters.

CIM or WMI cmdlets

Two families of cmdlets coexist. The WMI cmdlets (Get-WmiObject, Invoke-WmiMethod, Set-WmiInstance, Remove-WmiObject) date from PowerShell 1.0, use DCOM and no longer exist in PowerShell 7. The CIM cmdlets (the CimCmdlets module) have existed since PowerShell 3.0 and work in both versions.

Need Obsolete cmdlet CIM cmdlet
Read instances Get-WmiObject Get-CimInstance
Call a method Invoke-WmiMethod Invoke-CimMethod
Change an instance Set-WmiInstance Set-CimInstance
Delete an instance Remove-WmiObject Remove-CimInstance
Discover a class Get-WmiObject -List Get-CimClass
Remote session -ComputerName New-CimSession

The CIM cmdlets use WSMan (WinRM) by default, return lighter CimInstance objects and accept reusable sessions. The old ManagementObject objects carried their methods; a CimInstance does not, so you go through Invoke-CimMethod.

Discovering namespaces and classes

Most Win32_* classes live in root/cimv2, the default namespace. To list the child namespaces of a given namespace:

Get-CimInstance -Namespace 'root' -ClassName '__Namespace' |
    Select-Object -Property Name

Get-CimClass accepts wildcards and lets you explore the classes available on the machine:

Get-CimClass -ClassName 'Win32_*Disk*' |
    Select-Object -Property CimClassName

The structure of a class (properties, types, Key and ReadOnly flags, methods) is read like this:

$cimClass = Get-CimClass -ClassName 'Win32_LogicalDisk'
$cimClass.CimClassProperties |
    Select-Object -Property Name, CimType, Flags
$cimClass.CimClassMethods |
    Select-Object -Property Name

The pages of this site give the same information, together with the Microsoft descriptions.

Reading instances, filtering with WQL

Get-CimInstance returns every instance of a class. The -Filter parameter takes the WHERE clause of the WQL language (WMI Query Language), without the word WHERE:

Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DriveType = 3'

The filter is evaluated by the WMI provider: only the matching instances are transferred, which is faster than a Where-Object placed afterwards. For a full query, -Query accepts a SELECT statement:

$query = "SELECT Name, State FROM Win32_Service WHERE StartMode = 'Auto' AND State = 'Stopped'"
Get-CimInstance -Query $query

A few WQL rules:

  • the operators are =, <>, <, >, LIKE, AND, OR, NOT and IS NULL;
  • with LIKE, the wildcard is % (and _ for a single character);
  • strings are enclosed in apostrophes, and a backslash in a value is doubled: "Path = 'C:\\Windows'";
  • there is no ORDER BY and no join: sort afterwards with Sort-Object.

WQL strings contain apostrophes, which is why they are written in double quotes in the examples of this site.

Limiting the volume with -Property

By default WMI returns every property of every instance. On a large class or a slow network, -Property reduces what is transferred (key properties are always included):

Get-CimInstance -ClassName Win32_Process -Property Name, ProcessId, CommandLine

Select-Object is not a substitute for -Property: it restricts what is displayed, not what is queried.

CIM sessions: WSMan, DCOM and firewall

To query another computer, create a CIM session and reuse it. The session is closed in a finally block:

$cimSession = New-CimSession -ComputerName 'SERVER01'
try {
    Get-CimInstance -CimSession $cimSession -ClassName Win32_OperatingSystem
} finally {
    Remove-CimSession -CimSession $cimSession
}

The default protocol is WSMan (WinRM): TCP ports 5985 (HTTP) and 5986 (HTTPS). For an old host or one without WinRM, a session option forces DCOM, which uses port 135 and then a dynamic range of RPC ports:

$sessionOption = New-CimSessionOption -Protocol Dcom
$cimSession = New-CimSession -ComputerName 'SERVER01' -SessionOption $sessionOption

On the firewall side, allow the “Windows Remote Management” rules for WSMan, or “Windows Management Instrumentation (DCOM-In)” and “(WMI-In)” for DCOM. Test-WSMan -ComputerName SERVER01 quickly checks that WinRM answers.

A session also accepts an array of computers: Get-CimInstance -CimSession $sessions then queries all the machines in parallel.

Calling methods

A WMI method is called with Invoke-CimMethod. Parameters are passed in a hashtable and the result contains a ReturnValue property, whose codes are described on the method’s page:

$cimParameters = @{
    ClassName  = 'Win32_Process'
    MethodName = 'Create'
    Arguments  = @{ CommandLine = 'notepad.exe' }
    WhatIf     = $true
}
Invoke-CimMethod @cimParameters

For an instance method, pass the instance with -InputObject. For a static method, use -ClassName, as above. Cmdlets that change the system accept -WhatIf: use it first, then remove it once the result is checked. Set-CimInstance and Remove-CimInstance work the same way.

A process created by Win32_Process.Create runs in the session of the WMI provider, not in the user’s interactive session.

CIM dates

WMI dates are stored in DMTF format (20260101083000.000000+060). Get-CimInstance already converts them to [datetime]: ConvertToDateTime, which was needed with Get-WmiObject, is unnecessary.

Get-CimInstance -ClassName Win32_OperatingSystem |
    Select-Object -Property Caption, LastBootUpTime

In a WQL filter, however, the date must be in DMTF format. The ToDmtfDateTime method does the conversion:

$since = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddHours(-1))
Get-CimInstance -ClassName Win32_Process -Filter "CreationDate < '$since'"

Enumerations

Many properties return a numeric code: DriveType, State, Availability, the return codes of methods. The pages of this site list the documented values. A lookup table and a calculated property make the result readable:

$driveTypeName = @{
    2 = 'Removable Disk'
    3 = 'Local Disk'
    4 = 'Network Drive'
}
Get-CimInstance -ClassName Win32_LogicalDisk |
    Select-Object -Property @(
        'DeviceID'
        @{ Name = 'DriveType'; Expression = { $driveTypeName[[int]$_.DriveType] } }
    )

Bit fields are decoded with the -band operator: a set bit corresponds to a value in the list.

Performance

  • Filter in the query (-Filter or -Query) rather than with Where-Object.
  • Limit the properties with -Property.
  • Reuse a CIM session for several queries instead of passing -ComputerName on every call.
  • Avoid Win32_Product: every enumeration triggers the consistency check, and sometimes the repair, of every installed MSI package. Read the registry instead (Uninstall keys), as shown in the dedicated recipe.
  • For performance counters, prefer Get-Counter to the Win32_Perf* classes. The Win32_PerfRawData_* classes return raw values; the Win32_PerfFormattedData_* classes return computed values, but sampling is costly.
  • Set a timeout with -OperationTimeoutSec so that an unreachable host does not block a script.

Security and permissions

Querying a remote computer generally requires being a local administrator on the target. The minimum right can be delegated: membership of the “Remote Management Users” group for WinRM, then the “Enable Account” and “Remote Enable” permissions on the namespace concerned (wmimgmt.msc console, Security tab). With DCOM, the DCOM permissions come on top of the WMI ones.

New-CimSession -Credential lets you use another account. Never write credentials in clear text in a script: use Get-Credential or a secret store. With Kerberos, a second hop (a machine querying a third machine) does not inherit the credentials.

PowerShell 5.1 and PowerShell 7

Point Windows PowerShell 5.1 PowerShell 7
CimCmdlets module yes yes (Windows)
Get-WmiObject yes, obsolete absent
WSMan and DCOM sessions yes yes (Windows)
WMI on Linux or macOS no no

The examples of this site work in both versions.

PoshCode style

The examples of this site follow the PowerShell Practice and Style guide:

  • full names for cmdlets and parameters, never aliases (Select-Object, not select);
  • named parameters, never positional ones;
  • four-space indentation and “One True Brace Style” braces;
  • no trailing backtick: a pipeline continues after |, and a command that receives more than two parameters uses splatting ($cimParameters = @{ … } then @cimParameters);
  • single-quoted strings, except for interpolation or WQL containing apostrophes;
  • sizes converted with a calculated property, for example [math]::Round($_.Size / 1GB, 2).