Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

taskkill to differentiate 2 images by path

How to kill a process by name and orginiated from a particular path using taskkill?

taskkill /F /IM

certainly it cant differentiate 2 process started from two different locations C:\Dir1 and C:\Dir2

Does tasklist has any switch to get the path name

like image 407
NSN Avatar asked Nov 23 '12 06:11

NSN


3 Answers

taskkill cannot do it. But you could use PowerShell if it's an option:

(Get-WmiObject Win32_Process | Where-Object { $_.Path.StartsWith('C:\Dir1') }).Terminate()
like image 186
Joey Avatar answered Oct 01 '22 13:10

Joey


Use the following command (it works even without powershell):

wmic process where ExecutablePath='C:\\Dir1\\image.exe' delete

NOTE: ExecutablePath is accessable for all processes only if you run wmic as administrator on Windows 8

like image 39
l0pan Avatar answered Oct 01 '22 15:10

l0pan


Based on Joey's answer:

wmic Path win32_process Where "CommandLine Like '%C:\\Dir1\\image.exe%'" Call Terminate

This way I avoid NullReferenceException when Path is null (don't know why) and does not need PowerShell.

Ref: https://superuser.com/questions/52159/kill-a-process-with-a-specific-command-line-from-command-line


Warning

It is dangerous if there are other processes running with the commandline contains that image path. For example:

> start cmd /k "C:\windows\system32\notepad.exe"

> wmic Path win32_process where "CommandLine Like '%C:\\Windows\\system32\\notepad.exe%'" get caption,processid,executablePath,commandline
Caption      CommandLine                                ExecutablePath                   ProcessId
cmd.exe      cmd  /k "C:\windows\system32\notepad.exe"  C:\WINDOWS\system32\cmd.exe      11384
notepad.exe  C:\windows\system32\notepad.exe            C:\windows\system32\notepad.exe  9684

So... What if we use "C:\Dir1\image.exe%" instead of "%C:\Dir1\image.exe%"?

If we launched this program from Explorer, its commandline may be quoted. If we ignore it, there will be no matches:

> wmic Path win32_process where "CommandLine Like '%C:\\Windows\\system32\\notepad.exe%'" get caption,processid,executablePath,commandline
Caption      CommandLine                                ExecutablePath                   ProcessId
notepad.exe  "C:\WINDOWS\system32\notepad.exe"          C:\WINDOWS\system32\notepad.exe  108

> wmic Path win32_process where "CommandLine Like 'C:\\Windows\\system32\\notepad.exe%'" get caption,processid,executablePath,commandline
No Instance(s) Available.

Therefore, it is recommended to use "ExecutablePath" like l0pan's answer.

like image 45
Akira Yamamoto Avatar answered Oct 01 '22 13:10

Akira Yamamoto