Categories

WMI type accelerators

Something you don’t see used very often but that you need to be aware of are the WMI type accelerators. These were introduced as part of PowerShell 1.0 and have continued to be available in later versions. 

A type accelerator is a shortcut to using a .NET class – remember everything in PowerShell is >NET based – even WMI.  There are three accelerators:

[wmi]

[wmi]"root\cimv2:Win32_Process.Handle='5028'"

This is the same as

Get-WmiObject -Class Win32_Process -Filter "Handle=5028"

or

Get-CimInstance -ClassName Win32_Process -Filter "Handle=5028"

The draw back is that you have to use the WMI class Key which is Handle in Win32_Process. Get-WmiObject and Get-CimInstance give you a wider set of filtering options – the –Filter parameter will work on most properties

[wmiclass]

This provides a shortcut for creating new instances:

$p = [wmiclass]'Win32_Process'
$p.Create("calc.exe")

This was your only option in PowerShell 1.0 for accessing the Create method. With PowerShell 2.0 and 3.0 these options were added respectively:

Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList 'calc.exe'


Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{CommandLine='calc.exe'}

[wmisearcher]

The final WMI type accelerator can be used to find WMI data. You need to use a WQL query

$query = [wmisearcher]"SELECT * FROM Win32_Process WHERE Name='calc.exe'"
$query.Get()

Your alternatives are:

Get-WmiObject -Query "SELECT * FROM Win32_Process WHERE Name='calc.exe'"

Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE Name='calc.exe'"

OR better still

Get-WmiObject -Class Win32_Process -Filter "Name='calc.exe'"

Get-CimInstance -ClassName Win32_Process -Filter "Name='calc.exe'"

Lots of choices and one of these techniques will solve your problem when working with WMI.  More information is available in Chapter 3 of PowerShell and WMI – www.manning/siddaway2

Leave a Reply