Sunday, June 8, 2025

kill a process running on a specific port in Windows

 

Step 1: Find the PID (Process ID) using the port

Open Command Prompt (CMD) as Administrator and run:


netstat -aon | findstr :<PORT>

Replace <PORT> with the actual port number you're targeting.

๐Ÿ“Œ Example (for port 5000):


netstat -aon | findstr :5000

Look for a line like:


TCP 0.0.0.0:5000 0.0.0.0:0 LISTENING 1234

The last number (1234 in this example) is the PID.


๐Ÿงจ Step 2: Kill the process by PID

Once you have the PID, run:


taskkill /PID <PID> /F

Replace <PID> with the number from the previous step.

๐Ÿ“Œ Example:


taskkill /PID 1234 /F


Make a batch Script

Option 1: Batch Script (Simple & Quick)

You can save the following as kill_port.bat and run it as Administrator:

---------------------

@echo off set /p PORT=Enter port number to kill: for /f "tokens=5" %%a in ('netstat -aon ^| findstr :%PORT%') do ( echo Killing process with PID %%a taskkill /PID %%a /F ) pause


-------------------------------------

How to Use:

  1. Open Notepad.

  2. Paste the script above.

  3. Save as kill_port.bat (use Save as type: All Files).

  4. Right-click it and choose Run as Administrator.


Option 2: PowerShell Script (More Robust)

------------------------------------------------------------------------------

$port = Read-Host "Enter port number" $connection = netstat -aon | Select-String ":$port" if ($connection) { $pid = ($connection -split '\s+')[-1] Write-Host "Killing process with PID $pid" Stop-Process -Id $pid -Force } else { Write-Host "No process found using port $port" }



-------------------------------------------------------------------------------

How to Use:

  1. Save it as Kill-Port.ps1.
  2. Run it from PowerShell as Administrator.


No comments:

Post a Comment