Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 368
Peter Avatar asked Apr 10 '11 19:04

Peter


1 Answers

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 }
like image 186
Rosberg Linhares Avatar answered Sep 21 '22 09:09

Rosberg Linhares