header image

I’m afraid you can’t do that anymore

Posted by: | February 27, 2014 | No Comment |

In PowerShell 1.0 you could do this:

notepad
$proc = Get-WmiObject -Class Win32_Process -Filter "Name=’notepad.exe’"
$proc.Terminate()

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
‘Terminate’.
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

under: PowerShell and CIM, PowerShell and WMI, PowerShell Basics, PowerShell V3, PowerShell v4