Kill all processes by name with Powershell

A reader commented on a previous post pointing out a deficiency in one of the scripts used to kill processes on remote computers. If more than one instance of the specified process was running on the target computer the script would buckle. This is pretty easy to rectify. The below script will kill all process instances on the target machine.

# Kill all processes on a remote machine with a specific name
$computer = "localhost";
$processToKill = "notepad.exe";
$process = Get-WmiObject -Class Win32_Process -Filter "Name = '$processToKill'" -ComputerName $computer;
if($process -eq $null)
{              # If null then the process may not be running
                Write-Host -ForegroundColor Red "Couldn't get process $processToKill on $computer";
                sleep(10);
                exit;
}
else
{
                Write-Host "Attempting to Kill $processToKill on $computer";
}
 
# This original part of the script dies if $process is an array of more than one calc.exe process
# Kill the process and get exit status 0 = OK
# $status = $process.InvokeMethod("Terminate", $null);
# switch($status)
# {
#              0 { Write-Host -ForegroundColor Green "Killed $processToKill on $computer"};
#              default { Write-Host -ForegroundColor Red "Error, couldn't kill $processToKill on $computer"};
#};
 
$count = 1;
 
# This will work regardless if $process is an array or not
foreach ($ps in $process)
{
                Write-Host "Kill count = $count";
				Write-Host "Handle = " $ps.Handle;
                $status = $ps.InvokeMethod("Terminate", $null);
                switch($status)
                {
                                0 { Write-Host -ForegroundColor Green "Killed $processToKill on $computer"};
                                default { Write-Host -ForegroundColor Red "Error, couldn't kill $processToKill on $computer"};
                };
                $count++;
}

This will procedure output similar to below.

Attempting to Kill notepad.exe on localhost
Kill count = 1
Handle =  3788
Killed notepad.exe on localhost
Kill count = 2
Handle =  4916
Killed notepad.exe on localhost
Kill count = 3
Handle =  5884
Killed notepad.exe on localhost
Kill count = 4
Handle =  3488
Killed notepad.exe on localhost
Kill count = 5
Handle =  6232
Killed notepad.exe on localhost

A similar thing can be achieved , on the localhost, with a one-liner.

# Kills all processes called 'calc' on the localhost
ps calc | kill;

With a little bit of Remoting, available in Powershell V2, it would be simple enough to achieve the same functionality in the one-liner to execute this on remote computers.


Leave a Reply

Current day month ye@r *