taskkill通过路径区分2个图像

NSN*_*NSN 13 windows windows-shell

如何使用taskkill从名称和特定路径中删除进程?

taskkill/F/IM

当然它无法区分2个过程从两个不同的位置开始C:\ Dir1和C:\ Dir2

任务列表是否有任何开关来获取路径名称

Joe*_*oey 11

taskkill做不到.但是如果它是一个选项,你可以使用PowerShell:

(Get-WmiObject Win32_Process | Where-Object { $_.Path.StartsWith('C:\Dir1') }).Terminate()
Run Code Online (Sandbox Code Playgroud)

  • 为了严格相等,请使用 `$_.Path -eq "C:\Dir1\file.exe"`。作为参考,[此处](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process)是“Win32_Process”类属性的完整列表。 (3认同)

Aki*_*oto 6

基于乔伊的回答:

wmic Path win32_process Where "CommandLine Like '%C:\\Dir1\\image.exe%'" Call Terminate
Run Code Online (Sandbox Code Playgroud)

这样,当 Path 为空(不知道为什么)并且不需要 PowerShell 时,我可以避免 NullReferenceException。

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


警告

如果使用命令行运行的其他进程包含该图像路径,则很危险。例如:

> 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
Run Code Online (Sandbox Code Playgroud)

那么...如果我们使用“C:\Dir1\image.exe%”而不是“%C:\Dir1\image.exe%”会怎样?

如果我们从资源管理器启动这个程序,它的命令行可能会被引用。如果我们忽略它,将没有匹配项:

> 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.
Run Code Online (Sandbox Code Playgroud)

因此,建议使用“ExecutablePath”,如 l0pan 的回答


l0p*_*pan 6

使用以下命令(即使没有 powershell 也能工作):

wmic process where ExecutablePath='C:\\Dir1\\image.exe' delete
Run Code Online (Sandbox Code Playgroud)

注意:仅当您wmic在 Windows 8 上以管理员身份运行时,所有进程都可以访问 ExecutablePath

  • 原因是您使用的是 wmic,而不是批处理,即使它是批处理的,您正在使用命令 wmic,这确实需要您转义它,因为 wmic 中的 \ 与批处理中的不同。 (2认同)