Two processes with same name, how to close one that start most recent?

Ryan711

[H]ard|Gawd
Joined
Nov 16, 2004
Messages
1,173
If I have 2 processes with the same name, say for example, two instances of excel open, and I want to close them with the command line (or simple vbs script or something). They both would have the same name, excel.exe, but I only want to close the one that was opened most recently and not all instances. Is there anyway to acomplish this?
 
I don't know if you can pull the info from the command line, but you *should* be able to compare the process ID's. I guess there's no guarantee, but typically, the higher PID (larger value) was opened more recently.
 
If this is on Windows, then there is a guarantee: it's that process IDs are not ordered. You'll want to call GetProcessTimes() to find the start time for the processes, and figure out which one started first.
 
Can you use PowerShell?

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

One liner :)

Selects all running processes with Excel as the process name, sorts descending by start time, selects all but the first object (newest), and terminates them. If there's only one process running, it doesn't kill it.
 
PowerShell would be a good way to solve it with a scriptlet. But I can't get your code to work:

Code:
get-process |  sort-object -property "Starttime" -descending

gives
Code:
Sort-Object : Exception getting "StartTime": "Access is denied"
At line:1 char:27
+ get-process |  sort-object <<<<  -property "Starttime" -descending
    + CategoryInfo          : InvalidResult: (System.Diagnostics.Process (audiodg):PSObject) [Sort-Object], GetValueIn
   vocationException
    + FullyQualifiedErrorId : ExpressionEvaluation,Microsoft.PowerShell.Commands.SortObjectCommand

Sort-Object : Exception getting "StartTime": "Access is denied"
At line:1 char:27
+ get-process |  sort-object <<<<  -property "Starttime" -descending
    + CategoryInfo          : InvalidResult: (System.Diagnostics.Process (Idle):PSObject) [Sort-Object], GetValueInvoc
   ationException
    + FullyQualifiedErrorId : ExpressionEvaluation,Microsoft.PowerShell.Commands.SortObjectCommand

Sort-Object : Exception getting "StartTime": "Access is denied"
At line:1 char:27
+ get-process |  sort-object <<<<  -property "Starttime" -descending
    + CategoryInfo          : InvalidResult: (System.Diagnostics.Process (System):PSObject) [Sort-Object], GetValueInv
   ocationException
    + FullyQualifiedErrorId : ExpressionEvaluation,Microsoft.PowerShell.Commands.SortObjectCommand
 
It's a permissions issue. You can't enumerate certain properties on processes you don't have full rights to. Even if you run PowerShell as admin (looks like you did, or have UAC off), SYSTEM process, Idle process, and audiodg will throw that. It's kind of like how you have to elevate in task manager to see all running processes.

It should work fine for processes that the user has rights to, though.
 
Back
Top