How to kill all but the newest Excel process with PowerShell

Excel was the example I used, but you could use anything…Somebody on HardForums.com was asking about a way to kill all but the most recently started Excel process. I came up with this:

get-process | where-object { $_.name -eq "excel" } | sort-object -property "Starttime" -descending | select-object -skip 1 | foreach { taskkill /pid $_.id }

If there is just one excel process running, it will leave that process alone, thanks to the -skip 1 in Select-Object.

I can’t imagine this is too useful, but who knows? :)

1 comment to How to kill all but the newest Excel process with PowerShell

  • Aaron

    Even better:

    get-process excel | sort-object -property “Starttime” -descending | select-object -skip 1 | stop-process

    Or, using the default aliases:

    ps excel | sort -prop “Starttime” -desc | select -skip 1 | kill

    And a general killall is:

    ps excel | kill

    Short and sweet enough that there’s no need to add a function to my profile. :)

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>