bytama

ยท 1 min read

Find and kill the process holding a Windows port

netstat gives you the PID, taskkill ends it. The PowerShell equivalents are shorter.

EADDRINUSE, or "port 23104 is already in use", means some process is still listening. Windows does not make you guess: the socket table includes the PID.

Find the owner

netstat -ano | findstr :23104

A line looks like this:

  TCP    0.0.0.0:23104    0.0.0.0:0    LISTENING    15624

The last column is the process ID. -a includes listening sockets, -n keeps addresses numeric, -o adds the PID โ€” drop any of them and the output stops being grep-able.

Kill it

taskkill /PID 15624 /F

In PowerShell the same thing is shorter:

Stop-Process -Id 15624

taskkill /F is the equivalent of Stop-Process -Force: no chance for the process to save state. Use it for a dev server that ignored Ctrl+C, not for anything with unsaved work.

The native way

Get-NetTCPConnection returns objects, so you can skip the text parsing:

Get-NetTCPConnection -LocalPort 23104 | Select-Object OwningProcess

Then Stop-Process -Id <pid>. This is the version worth putting in a script: if the port is free it returns nothing instead of an error you have to filter.