29

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?

rpeshkov
  • 4,877
  • 3
  • 27
  • 43

5 Answers5

57

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
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 3
    -WhatIf was just a safety switch :) – Shay Levy Apr 22 '12 at 10:35
  • 5
    fyi if you match all processes with `-like "*"` and execute the command, it crashes your system – Oleg Sep 30 '12 at 23:44
  • 1
    According to the [Invoke-WmiMethod documentation](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/invoke-wmimethod), you should use the new Common Information Model (CIM) cmdlets, introduced in Windows PowerShell 3.0: `Get-CimInstance Win32_Process -Filter "ExecutablePath LIKE '%something%'" -ComputerName server1 | Invoke-CimMethod -Name Terminate` – Rosberg Linhares Dec 06 '16 at 20:51
  • I find that `-WhatIf` option can not kill the process for me, I have to use the `-Force` option to kill the process. – jdhao Aug 19 '22 at 10:08
9

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}
mpasko256
  • 811
  • 14
  • 30
  • While this worked for me (as @Shay Levy's answer didn't work) it looks like it's stuck in a loop - you need to cancel the operation (`Ctrl+C` should be enough) to get back to the console – Mache Jan 23 '19 at 15:43
  • @Mache I've just added `-Force` flag but I doubt if it would really help. Please take a look also at: https://stackoverflow.com/questions/40585754/powershell-wont-terminate-hung-process – mpasko256 Jan 24 '19 at 13:25
  • What is -processname ? It is not documented? https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/stop-process?view=powershell-5.1 – kuga May 18 '20 at 09:42
  • @kuga As I see, it was an alias for -Name: https://learn.microsoft.com/en-us/previous-versions/powershell/module/microsoft.powershell.management/stop-process?view=powershell-4.0 – mpasko256 May 18 '20 at 14:53
0

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.

David Z.
  • 5,621
  • 2
  • 20
  • 13
0

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

Stop-Process -processname notepad
Roland
  • 972
  • 7
  • 15
0

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.

dzmanto
  • 216
  • 2
  • 6