PowerShell for Windows Admins

Mar 18 2014   1:05PM GMT

WMI type accelerators

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: