Enumerate multiple processes and kill all but one?

I have an application that launches when a user logs in via the Startup directory.  This works OK until a second user inadvertently logs in at the same time, which can cause the application to crash.  I've seen some examples of using the Get-Process comandlet to enumerate the instances of a process (although none are too elegant), but I haven't seen anything that shows how to kill only one specific instance of the process.  Can anyone help a poor network guy?

Thanks,
Joe

September 10th, 2015 4:37pm

Kill one specific instance - what do you mean? How do you know which one to stop?
Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 4:49pm

maybe based on owner? You should try then different option 

get-wmiobject -class win32_process | ? {$_.name -match "processname" -and $_.getowner().user -match "USERNAME" } | %{ $_.terminate() } 


if it's on remote computer then use  -comptuername

September 10th, 2015 5:05pm

Thanks for the quick replies. This is currently running on a 2003 server but will be migrated to 2012 in about 2 weeks. That will probably make a difference on the approach.  Since I wouldn't know which user was the second to log in, I would have do something to NOT kill the original process who's owner I know.  Does this make sense?

Joe

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 5:13pm

So you want to leave the one running that started first?

You could loop the processes in PowerShell, keep track of which one has the oldest start time, and then stop the others. Example:

September 10th, 2015 6:09pm

Use "exehdr" to tag the process as "single instance".  That will allow only the fist instance to run.  A second attempt will just shift focus to the first instance.

It is also possible to tag the registry to control single instance.

Post in developer forum for instructions and how to get the exehdr utility.  Just create one copy and copy it to all locations that are using the EXE.

You can also assume that the PID that is lower is first although I suggest checking for the existence of the process will be all you need.

It is not possible for a standard user to see another users process so most of these ideas will not likely work.  Tagging the header will work.  Any method that works on current system will work on WS2012.

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 6:25pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics