S.Stevens
S.Stevens

Reputation: 418

Kill Particular Instance of an .exe by name using powershell

I am attempting to use powershells'

Stop-Process -Name "notepad.exe"

Type command to kill, in the above case, all instances of notepad.exe even if I had 10 notepad files open.

I could of course use:

Stop-Process -Id 3952

To kill a particular instance of it, but if I were to then re-open all 10 notepad files the PID's would have changed. I can see in task manager each instance of notepad has a different process name, a name that will not change when I re-open it:

enter image description here

Is there a way, using a powershell script, to specifically kill "Notes 2" for example?

Upvotes: 1

Views: 511

Answers (1)

boxdog
boxdog

Reputation: 8432

You can use the MainWindowTitle property returned by Get-Process to do this. For example:

Get-Process |
    Where-Object MainWindowTitle -eq 'Notes 2 - Notepad' |
        Stop-Process -Force

Of course, if you had the same document open in more than one Notepad instance, they would have the same title and you'd be back to your original problem where you can't tell them apart.

Upvotes: 2

Related Questions