I’m afraid you can’t do that anymore

In PowerShell 1.0 you could do this:

$proc = Get-WmiObject -Class Win32_Process -Filter “Name=’notepad.exe'”

To access the methods of the WMI class you had to get a variable representing the instance and call the method. This technique still works in PowerShell 4.0

When the CIM cmdlets were introduced in PowerShell 3.0 people assumed that they worked the same:

£> notepad
£> $proc = Get-CimInstance -ClassName Win32_Process -Filter “Name=’notepad.exe'”
£> $proc.Terminate()
Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named
At line:1 char:1
+ $proc.Terminate()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Terminate:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

The CIM cmdlets retrun inert objects – NO METHODS – so you can’t do that.

The correct way to use the CIM cmdlets is:

Get-CimInstance -ClassName Win32_Process -Filter “Name=’notepad.exe'” | Invoke-CimMethod -MethodName Terminate

This entry was posted in CIM, PowerShell and WMI, Powershell Basics, PowerShell V3, PowerShell v4. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s