在命令行中终止进程树的进程 (Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5614164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:15:53  来源:igfitidea点击:

Terminate a process of a process tree in command line (Windows)

windowscommand-linekill

提问by Peter

I am in need of a command that will allow me to terminate a process of a process tree.

我需要一个命令来终止进程树的进程。

For example notepad.exewas created by explorer. How would I terminate the notepad.exein the explorer.exeprocess tree ?

例如notepad.exe是由资源管理器创建的。我将如何终止notepad.exeexplorer.exe进程树?

回答by Alois Kraus

Use taskkill /IM <processname.exe> /T.

使用taskkill /IM <processname.exe> /T.

回答by Ben

taskkill /F /IM notepad.exe

This would kill all notepad.exe's -- if you want a way to specify to only kill notepad.exe's which were created by foo.exeor so, I do not think Windows command lines are powerful enough for this.

这将杀死所有notepad.exe的 - 如果您想要一种方法来指定仅杀死notepad.exefoo.exe左右创建的,我认为 Windows 命令行不够强大。

You might can use tasklistto get the process ID of the process you want to target and then use taskkill /F /PID <PID>to kill it.

您可以使用tasklist获取要定位的进程的进程 ID,然后使用taskkill /F /PID <PID>它来终止它。

回答by Rosberg Linhares

Nowadays, you can do this with PowerShell:

现在,您可以使用 PowerShell 执行此操作:

$cimProcesses = Get-CimInstance -Query "select ProcessId, ParentProcessId from Win32_Process where Name = 'notepad.exe'"
$processes = $cimProcesses | Where-Object { (Get-Process -Id $_.ParentProcessId).ProcessName -eq "explorer" } | ForEach-Object { Get-Process -Id $_.ProcessId }

$processes.Kill()

Or the graceful way:

或者优雅的方式:

$cimProcesses = Get-CimInstance -Query "select ProcessId, ParentProcessId from Win32_Process where Name = 'notepad.exe'"
$cimProcesses = $cimProcesses | Where-Object { (Get-Process -Id $_.ParentProcessId).ProcessName -eq "explorer" }

$cimProcesses | ForEach-Object { taskkill /pid $_.ProcessId }

回答by Kirill V. Lyadvinsky

Try PsKill + PsList utilities from the PsTools set.

尝试从PsKill +则PsList公用事业PsTools集

pslist -twill give you a process tree (here you can find notepad.exewhich is a child process of the explorer.exe. Then you can use pskillto kill the process with specified id.

pslist -t会给你一个进程树(在这里你可以找到notepad.exe哪个是 的子进程explorer.exe。然后你可以用pskill指定的id杀死进程。

回答by Tonatio

This answer is not exactly for this case, but it can be useful for people looking how to kill the whole process tree. We need to combine some answers here to get the proper result: kill the process tree and force it.

这个答案并不完全适用于这种情况,但对于寻找如何杀死整个进程树的人来说可能很有用。我们需要在这里结合一些答案以获得正确的结果:杀死进程树并强制它。

taskkill /IM "<process-name>" /T /F

taskkill /IM "<process-name>" /T /F

For some processes you need to run the command in a console application with administrator rights.

对于某些进程,您需要在具有管理员权限的控制台应用程序中运行该命令。