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

问题描述 投票:8回答:4

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

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

windows command-line kill
4个回答
6
投票

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


2
投票

尝试使用PsTools set中的PsKill + PsList实用程序。

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


2
投票
taskkill /F /IM notepad.exe

这将杀死所有notepad.exe - 如果你想要一种方法来指定只杀死由notepad.exe创建的foo.exe,我不认为Windows命令行对此有足够的强大。

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


0
投票

现在,您可以使用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()

或者优雅的方式:

$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 }
© www.soinside.com 2019 - 2024. All rights reserved.