Powershell script to kill long-running processes

Here’s a small script to kill all Word or Excel-processes that have been running for more than 5 minutes. This can be useful on a server that launches those applications to generate documents. You probably shouldn’t do it that way but if you do you’re likely to discover that the processes don’t always terminate as they should. In those situations, a script like this can come in handy.

$startTimeLimit = (get-date) - (new-timespan -minutes 5)
$processes_to_kill = get-process | 
    where {$_.StartTime -lt $startTimeLimit -and ($_.path -like "*winword.exe" -or $_.path -like "*excel.exe") }
if ($processes_to_kill -ne $null)
    $processes_to_kill | foreach { $_.Kill() }


12 thoughts on “Powershell script to kill long-running processes”

  1. This is great 🙂 Worked with Win7x64. I simplified your script a bit:

    $startTimeLimit = (get-date) – (new-timespan -minutes 5)

    get-process | where {$_.StartTime -lt $startTimeLimit -and ($_.path -like “*winword.exe” -or $_.path -like “*excel.exe”) } | foreach { $_.Kill() }


    your script was perfect for my problem, working on windows server 2003 on AWS.

  3. we have ascript like this :

    # PowerShell
    #Get list of processes matching the name and older than 30 minutes.
    $orphanProcs = get-process | where {($_.path -like “*chrome.exe” -or $_.path -like “*iexplore.exe” -or $_.path -like “*firefox.exe”
    -or $_.path -like “*IEDriverServer.exe” -or $_.path -like “*chromedriver.exe”) -and
    ($_.StartTime -lt (get-date).addminutes(-30))}

    #Check if list is Null and if not kill them all:
    If ($orphanProcs) {
    #display list
    #kill list
    $orphanProcs | foreach { $_.Kill() }
    } Else {
    echo “no processes found older than specified”

    but it kills all the browser session rather than those which are older than 30 mins.

    We are on windows 2012 r2 version . Can someone help please

  4. @rocky, the script looks okay. But maybe you have dependencies between the processes, such as an old chromedriver.exe having started new chrome.exe processes so that when you kill chromedriver.exe, then also the newer chrome.exe processes are killed?

    I would also check your script carefully so you don’t have any unwanted newlines. You can also try adding extra grouping parenthesis, just in case. For example around the whole right hand part of the assignment:

    $orphanProcs = ( get-process | where {…} )

    Other than that, what normally helps with this type of problem is to remove parts of the script one at a time, until it starts working. The last part you remove is probably causing it. It might be in your where clause, for example.



  5. Thanks Emil For your help but I am still not able to achieve my task…

    I have modified the script to kill sessions older than 10 minutes and create log also and in this scenario I have included chrome browser, excel and word file .

    My Script is :

    $startTimeLimit = (get-date) – (new-timespan -minutes 10)
    $processes_to_kill = get-process |
    where {$_.StartTime -lt $startTimeLimit -and ($_.path -like “*winword.exe” -or $_.path -like “*chrome.exe” -or $_.path -like “*excel.exe”) }
    if ($processes_to_kill -ne $null)
    $processes_to_kill | foreach { $_.Kill() }

    $Stamp = (Get-Date).toString(“yyyy/MM/dd HH:mm:ss”)
    $Separator = “******************************************************************************************************************************”
    $Counter = “0”
    if ($processes_to_kill -ne $null)
    $Counter = @($processes_to_kill).Count
    Add-Content ‘C:\Users\n427193\joblogfile.txt’ $Stamp
    Add-Content ‘C:\Users\n427193\joblogfile.txt’ ‘Total number of browser session older than 30 minutes are :’
    Add-Content ‘C:\Users\n427193\joblogfile.txt’ $Counter
    Add-Content ‘C:\Users\n427193\joblogfile.txt’ $Separator

  6. In case of excel and word it works perfectly fine but unfortunately fails in case of chrome browser as if it finds even a single browser session older than 10 minutes it closes all the sessions, which are even not older than 10 minutes.

    My final objective is close all the sessions of firefox, chrome, internet explorer session which are older tan 30 minutes.

    If you can help then it will be great!!!!

  7. That sounds like a problem caused by how Chrome manages it’s processes. I don’t know the details about that, I’m afraid.


  8. Great Script. Is there any idea to kill only a particular excel file rather than killing all.

  9. (get-process |?{($_.StartTime -lt (get-date) – (new-timespan -minutes 180)) -and ($_.ProcessName -like “Powershell”) }).Kill()

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

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