PowerShell script to kill named processes

There are times when you need to kill a number of processes in one-go like today when Chrome crashed a few times hanging all the running instances – next time Google says, one tab cannot bring down all of them – send them my way :). For such times, a PowerShell script is all you need.

I wrote up a simple one which takes the process name as input and then kills all the processes which match that name.

#Script is not signed, so need this.
Set-ExecutionPolicy Unrestricted

#Need to set the param to a variable
$target = $args[0]

if($target) {
    $orphanProcs = get-process | where {$_.Name -eq $target}

    #Check if list is null; if not kill all the procs
    if ($soonToBeDeadProcs) {
        #display list
        $soonToBeDeadProcs

        #kill list
        $soonToBeDeadProcs | foreach { $_.Kill() }
    }
    else { Write-Host "Oops, no processes found older with the name: $target" }
}
else { 
    Write-Host "Oops, no arguments passed. You need to provide one argument (the Process Name)."
    Write-Host "Example 1: killproc chrome"
    Write-Host "Example 2: killproc 'some other process'"
}

Example Output (Killing Chrome in this case):

PS C:\Users\amit.bahree\Desktop> .\killproc.ps1 chrome

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
——-  ——    —–      —– —–   ——     — ———–
    139      30    34292      46708   164     3.87    376 chrome
    137      22    20932      33648   149     2.48   1260 chrome
    141      21    17896      31328   148     3.01   3572 chrome
   5434      37    56932      66528   266 1,134.36   4940 chrome
    139      22    20288      33084   150     4.12   5032 chrome
    145      21    16576      31368   149     0.58   5148 chrome
    147      19    14384      26992   150     1.42   5604 chrome
    142      23    32292      37416   156     8.42   6528 chrome
    136      17    12456      23964   142     0.30   6732 chrome
    144      26    27004      39136   156     0.98   6736 chrome
   1586      90   151224     209888   512   395.87   7184 chrome
    138      22    21388      33916   151     3.76   7504 chrome
    123      13     7756      15196   126     0.56   7512 chrome
    142      21    23112      35552   150     2.01   9860 chrome
    140      18    13032      25148   150     1.73  10432 chrome

Published by

Amit Bahree

This blog is my personal blog and while it does reflect my experiences in my professional life, this is just my thoughts. Most of the entries are technical though sometimes they can vary from the wacky to even political – however that is quite rare. Quite often, I have been asked what’s up with the “gibberish” and the funny title of the blog? Some people even going the extra step to say that, this is a virus that infected their system (ahem) well. [:D] It actually is quite simple, and if you have still not figured out then check out this link – whats in a name?

One thought on “PowerShell script to kill named processes”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.