12

I am going over this process many times. I have to kill a process on a Windows machine and I just know its port. Normally the steps are as below: Find the PID by looking at the ports (example port 8084) List the processes running on ports

netstat -a -o -n

And then kill the process on that port with the following command

taskkill /F /PID <pid>

Is there any thing like a pipe or similar that I can use on Windows OS to run this command in one line!?

2
  • 1
    I'm sorry that I can't write a full answer right now, but have a look at findstr (the Windows grep). For example: netstat -a -o -n | findstr "LISTENING" | findstr ":135". Maybe this gets you a step closer ;) Jul 30, 2012 at 10:20
  • @OliverSalzburg Need to be careful with that 2nd findstr: might match either local or remote port number. (Accepting that the Q doesn't specify which.)
    – Richard
    Jul 30, 2012 at 10:27

3 Answers 3

12

Is there any thing like a pipe or similar that I can use on Windows OS to run this command in one line?

Both cmd.exe and PowerShell support pipes from one command to another. In PowerShell something like (this should be on a single line on the command line, or use ` to escapte newlines in a script):

netstat -ano
 | select -skip 4 
 | % {$a = $_ -split ' {3,}'; New-Object 'PSObject' -Property @{Original=$_;Fields=$a}} 
 | ? {$_.Fields[1] -match '15120$'}
 | % {taskkill /F /PID $_.Fields[4] }

Where:

  • Select -skip 4 skips the first four header lines. (Select is short for Select-Object used to perform SQL SELECT like projects of objects.
  • % is short for Foreach-Object which performs a script block on each object ($_) in the pipeline and outputs the results of the script block to the pipeline. Here it is first breaking up the input into an array of fields and then creating a fresh object with two properties Original the string from netstat and Fields the array just created.
  • ? is short for Where-Object which filters based on the result of a script block. Here matching a regex at the end of the second field (all PowerShell containers a zero based).

(All tested except the last element: I don't want to start killing processes :-)).

In practice I would simplify this, eg. returning just 0 or the PID from the first foreach (which would be designed to ignore the headers) and filter on value not zero before calling taskkill. This would be quicker to type but harder to follow without knowing PowerShell.

3
  • for some reason my powershell does not recognize the select! Running Windows 7 "Der Befehl "select" ist entweder falsch geschrieben oder konnte nicht gefunden werden." Same for "Select-Object"
    – Armand
    Jul 30, 2012 at 14:11
  • This worked for me, but the process is found twice, I believe due to IPv4 and IPv6 versions being listed by netstat. This leads to a (probably harmless) error of ERROR: The process "12345" not found.. Jan 13, 2017 at 1:21
  • I think the important takeaway here is taskkill /F /PID [PID]. With that, someone can manually kill tasks so long as they have a way of getting the PID.
    – Sawtaytoes
    Mar 2, 2017 at 0:39
3

Open command prompt and execute:

for /f "tokens=5" %a in ('netstat -aon | find "8080"') do taskkill /f /pid %a

If you want to do it in a batch file instead, replace %a with %%a and | with ^|.

If you just want to kill the one that is listening on that port append | find "LISTENING" at the end of the other find.

1

Here is a convenient function that you can use:

https://github.com/majkinetor/posh/blob/master/MM_Network/Stop-ProcessByPort.ps1

killp 8081

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .