cmac
cmac

Reputation: 1

Powershell Stop-Process Many Instances

I have a remote access program that does not clean up after itself after it is closed. In Task Manager, I oftentimes find 5 to 10 instances of the program running. For instance:

I have a simple Powershell script to stop these processes, but the problem is I want to close n-1 out of n processes.

> Stop-Process -Force -Name XYZ*

kills n out of n processes.

Is there a way to kill all processes of a program while leaving open the newest (e.g. XYZ.exe #5)?

Upvotes: 0

Views: 715

Answers (2)

Francesco Mantovani
Francesco Mantovani

Reputation: 12377

Try this: it closes all non responding processes

Get-Process -name XYZ.exe| Where-Object -FilterScript {$_.Responding -eq $false} | Stop-Process

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174900

Use Get-Process to discover all matching processes ahead of time, then simply remove one of them before killing the rest:

Get-Process -Name XYZ* |Select -Skip 1 |Stop-Process -Force

Upvotes: 2

Related Questions