WMI Quick Primer (Part 4)

by May 8, 2018

Typically, WMI objects contain properties with valuable information. This line would retrieve all information about all running Notepad instances (make sure you launch Notepad before running this):

Get-WmiObject -Class Win32_Process -Filter 'Name LIKE "%notepad%"' 

Likewise, this would use Get-CimInstance to get the same information:

Get-CimInstance -Class Win32_Process -Filter 'Name LIKE "%notepad%"' 

Sometimes, WMI objects can also contain methods. The easiest way to find the method names is to use Get-WmiObject, and pipe the results to Get-Member:

 PS> Get-WmiObject -Class Win32_Process -Filter 'Name LIKE "%notepad%"' | Get-Member -MemberType *method TypeName: System.Management.ManagementObject#root\cimv2\Win32_Process Name MemberType Definition ---- ---------- ---------- AttachDebugger Method System.Management.ManagementBaseObject AttachDebugger() GetAvailableVirtualSize Method System.Management.ManagementBaseObject GetAvailableVirtualSize() GetOwner Method System.Management.ManagementBaseObject GetOwner() GetOwnerSid Method System.Management.ManagementBaseObject GetOwnerSid() SetPriority Method System.Management.ManagementBaseObject SetPriority(System.Int32 Priority) Terminate Method System.Management.ManagementBaseObject Terminate(System.UInt32 Reason) ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime(); ConvertToDateTime ScriptMethod System.Object ConvertToDateTime(); 

To call a method, and for example get the owner of a process, there is a fundamental difference between Get-WmiObject and Get-CimInstance:

When you retrieve objects via Get-WmiObject, the methods are part of the returned objects:

$notepads = Get-WmiObject -Class Win32_Process -Filter 'Name LIKE "%notepad%"' $notepads | ForEach-Object { $notepads.GetOwner() } 

Objects returned by Get-CimInstance do not contain WMI methods. To invoke WMI methods, you need to call Invoke-CimMethod:

$notepads = Get-CimInstance -Class Win32_Process -Filter 'Name LIKE "%notepad%"' $notepads | ForEach-Object { Invoke-CimMethod -InputObject $_ -MethodName "GetOwner" } 

In the code above, when you remove “GetOwner” and instead press CTRL+SPACE to invoke IntelliSense (or press TAB in a simple PowerShell console), you get full IntelliSense on available method names. If a method requires arguments, use the -Arguments parameter.

Twitter This Tip! ReTweet this Tip!