rpeshkov
rpeshkov

Reputation: 5047

Kill process by filename

I have 3 instances of application running from different places. All processes have similar names.

How can I kill process that was launched from specific place?

Upvotes: 29

Views: 40134

Answers (5)

mpasko256
mpasko256

Reputation: 841

I would like to slightly improve Shay Levy's answer, as it didn't work work well on my setup (version 4 of powershell)

Get-Process | Where-Object {$_.Path -like "*something*"} | Stop-Process -Force -processname {$_.ProcessName}

Upvotes: 10

dzmanto
dzmanto

Reputation: 236

The below command kills processes wherein "something" is part of the path or is a command line parameter. It also proves useful for terminating powershell scripts such as
powershell -command c:\my-place\something.ps1 running something.ps1 from place c:\my-place:

gwmi win32_process | Where-Object {$_.CommandLine -like "*something*"}  | % { "$(Stop-Process $_.ProcessID)" }

The solution works locally on my 64bit Windows 10 machine.

Upvotes: 0

Roland
Roland

Reputation: 1010

Try this: http://technet.microsoft.com/en-us/library/ee177004.aspx

Stop-Process -processname notepad

Upvotes: 0

Shay Levy
Shay Levy

Reputation: 126732

You can get the application path:

Get-Process | Where-Object {$_.Path -like "*something*"} | Stop-Process -WhatIf

That will work for the local machine only. To terminate remote processes:

Get-WmiObject Win32_Process -Filter "ExecutablePath LIKE '%something%'" -ComputerName server1 | Invoke-WmiMethod -Name Terminate

Upvotes: 59

David Z.
David Z.

Reputation: 5701

You can take a look at the MainModule property inside of the Process class (which can be invoked via powershell).

foreach (Process process in Process.GetProcesses())
{
   if (process.MainModule.FileName == location)
   {
       process.Kill();
   }
}

I'd also consider the possible exceptions that can occur while calling this code. This might occur if you're trying to access processes that are no longer present (killed since the last time GetProcess was called) or processes for while you do not have permissions.

Upvotes: 0

Related Questions