50 Ways to Kill a Process

Monday, July 17, 2006

I've been learning PowerShell and alternating between frustration and fascination. PowerShell is potent but comes with a learning curve. ScottGu recently posted about using tasklist and taskkill to stop an ASP.NET worker process, and I thought it might be a good experience to give this a whirl at the PS prompt.

Cmdlets are the built-in commands of PowerShell. The get-process cmdlet is equivalent to tasklist, and can return all running processes:

PS> get-process | where { $_.Name -eq "w3wp" }

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    480      14    26288      11552   159     1.52   2580 w3wp
    351      21    12464      22360   111     0.45   4616 w3wp

Here we've filtered the process list by piping into the where-object cmdlet. The two worker processes pass the filter and appear in the output.

One of the significant concepts behind PS is that it doesn't pipe plain text - it passes objects in the pipeline. The get-process cmdlet returns a collection of System.Diagnostics.Process objects - the same Process class we know and love from the .NET base class library, and the same class that has a Kill method.

PS> get-process | where { $_.Name -eq "w3wp" } |
    foreach { $_.Kill() }

$_ represents the current pipeline object, so the above command will invoke the Kill method of each Process object that makes it's way through the filter. We can use some shortcuts for less typing.

PS> get-process w3wp | stop-process

This filters the process list using a parameter to get-process, and pipes the output into a stop-process cmdlet. If this still seems too wordy, we can take a couple more shortcuts and use the get-process alias of gps, and the stop-processes alias of kill:

PS> gps w3wp | kill

Tune in tomorrow when we find the application pool name and uptime of each worker process.

The problem is all inside your head
She said to me
The answer is easy if you
Take it logically
I’d like to help you in your struggle
To be free
There must be fifty ways
To kill a process

CHORUS:
You need a PowerShell hack, Jack
Do a task scan, Stan
You don’t need to destroy, Roy
Just get yourself free
Write in C++, Gus
You don’t need to discuss much
Just drop the PID, Lee
And get yourself free…

Apologies to Paul Simon.


Comments
Jeffrey Snover Monday, July 17, 2006
I would love to hear what you find frustrating and how/where we can do a better job.

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: www.microsoft.com/.../msh.mspx
scott Tuesday, July 18, 2006
Jeff:

Part of the frustration is just getting into the PowerShell "mindset", I'd call it. It took me some time and experimentation get over a hump. The formatting sometimes drives me crazy (I haven't figured out what the algorithm is for column width and alignment).

If I had a guided tour complete with excersices that get increasingly more sophisticated, I think that would help a lot.
Robert W. Anderson Monday, July 24, 2006
I was looking for a "just kill of the PID, Sid", but I like your version better.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!