Powershell stop process and keep latest running

Hello,

I am new to powershell. I need a script to kill multiple instances of a process except for the most latest instance. 

Just kill older instances but keep the latest instance running.

Any responses will be much appreciated.

Thanks!

Shaishav

July 8th, 2015 4:24pm

The newest is the one with the highest process ID.

$newest=get-process svchost |sort id -desc|select -first 1

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 4:42pm

In theory:

1. Enumerate processes according to name vs PID

2. Retrieve the "age" of process instances

http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/27/powertip-use-powershell-to-easily-find-how-long-a-process-runs.aspx

3. Kill instances until the "youngest one" is the last living process

M.

July 8th, 2015 5:05pm

Thanks jrv. This helped!
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 5:37pm

Thanks Milos. Very helpful
July 8th, 2015 5:37pm

In theory:

1. Enumerate processes according to name vs PID

2. Retrieve the "age" of process instances

http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/27/powertip-use-powershell-to-easily-find-how-long-a-process-runs.aspx

3. Kill instances until the "youngest one" is the last living process

M.

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 8:57pm

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

Other recent topics Other recent topics