开发者

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

开发者 https://www.devze.com 2023-02-23 04:19 出处:网络
I am in need of a command that will allow me to terminate a process of a pro开发者_如何学Ccess tree.

I am in need of a command that will allow me to terminate a process of a pro开发者_如何学Ccess tree.

For example notepad.exe was created by explorer. How would I terminate the notepad.exe in the explorer.exe process tree ?


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

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


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


Nowadays, you can do this with 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 }


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.exe or so, I do not think Windows command lines are powerful enough for this.

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


Try PsKill + PsList utilities from the PsTools set.

pslist -t will give you a process tree (here you can find notepad.exe which is a child process of the explorer.exe. Then you can use pskill to kill the process with specified id.

0

精彩评论

暂无评论...
验证码 换一张
取 消