How to stop (terminate) processes on a remote server using PowerShell and CIM / WMI?

My friends asked me simple question: How to stop (terminate) processes on a remote server?

Of course you can use:

Invoke-Command -ComputerName RemoteServer


Invoke-Command -Session $existingSessionToRemoteServer

But in this case my friends had established CIM session in their script. I wrote a few simple examples how to stop process using WMI and CIM and if you add -ComputerName (for Get-WmiObject) or -CimSession (for Get-CimInstance) then you can do it on a remote server.

WMI on local computer

(Get-WmiObject Win32_Process -Filter "Name = 'calc.exe'").Terminate()

CIM on remote computer

$processItems = Get-CimInstance `
    -ClassName Win32_Process `
    -Filter "Name = 'calc.exe'" `
    -CimSession ''

if ($processItems ) {
    # This will not work
    # $a.Terminate()

    foreach ($processItem in $processItems) {
        Write-Verbose -Message ('{0}: Terminate process: {1} ({2})' -f $processItem.Name, $processItem.ProcessId) -Verbose

        $result = Invoke-CimMethod -InputObject $processItem -MethodName Terminate

        if ($result.ReturnValue -ne 0) {
            Write-Error -Message ('Error durring trial to terminate process; ReturnValue: {0}' -f $result.ReturnValue)
} else {
    Write-Verbose -Message 'Nothing to terminate' -Verbose

Leave a Reply

Your email address will not be published.