How to Find and Kill a Process Using a Specific Port on Windows

windows, command-line
May 18, 2023

Sometimes, you may encounter an error that says “address already in use” when trying to run a program that uses a specific port. This means that another process is already using that port. In this post, we’ll go over the steps to find and kill the process using that port on Windows.

1. Find the Process ID (PID) of the process using the port:

To do this, open the Command Prompt and enter the command

> netstat -aon | findstr :[port number]

replacing [port number] with the actual port number you’re interested in. This will show you the PID of the process using that port.

2. Kill the process:

To kill the process, you can enter the command

> taskkill /F /PID [PID] 

replacing [PID] with the actual PID of the process you want to kill. This will forcefully terminate the process.

And that’s it! Now you should be able to run your program without encountering the “address already in use” error.

Piped commands

Putting the above things together run the following command with your port number to find the kill the process using it.

> netstat -ano | Select-String '[port number]' | ForEach-Object { $_ -split '\s+' } | Select-Object -Index 4 | ForEach-Object { Stop-Process -Id $_ -Force }

PowerShell script

To make it reusable, we can write the above command in a PowerShell script as follows

param (
    [Parameter(Mandatory = $false, Position = 0)]
    [ValidateRange(1, 65535)]
    [int]$PortNumber = 443,

if ($Help) {
    Write-Output "Usage: KillPort [-Help] [<PortNumber>]"
    Write-Output ""
    Write-Output "Description: Terminates processes using the specified port number."
    Write-Output ""
    Write-Output "Options:"
    Write-Output "  -Help              Show this help information."
    Write-Output "  <PortNumber>       Specify the port number to target (default: 443)."

$connections = netstat -ano | findstr "$PortNumber"
if (-not $connections) {
    $connections = netstat -ano | findstr "$PortNumber"

if ($connections) {
    $pids = $connections | ForEach-Object { $_ -split '\s+' } | Select-Object -Index 5
    $pids | ForEach-Object { taskkill /F /PID $_ }
    Write-Output "Processes using port $PortNumber have been terminated."
} else {
    Write-Output "No processes found using port $PortNumber."

Note: In addition to checking$portNumber, it is also useful to check$portNumber

Invoke the above PowerShell scripts using the following command (assuming TerminateProcessByPort.ps1` is the name of the file.

> .\TerminateProcessesByPort.ps1 -PortNumber 443
Ganessh Kumar R P © 2022 • All rights reserved.