How to Kill the Process Using a port on Localhost in Windows

In this tutorial, you will learn how to free a port that you want to use on localhost thereby resolving the conflict.

Killing the Process Using a port on Localhost in Windows
Killing the Process Using a port on Localhost in Windows

Windows uses the built-in TCP and UDP protocols to run different processes on different ports on localhost. If a port is in use and you execute a process that requires that same port, you will end up with an error warning you that the port is in use. This means that you either use an alternative port or kill the process on the port that you need to use on localhost.

You might also find yourself in a situation where the process on a specific port on localhost becomes unresponsive and needs to be terminated. If you find yourself in similar situations or you just want to free up system resources and resolve conflicts, this article is for you.

We will show you different methods to kill a process that is using a port on Localhost in Windows.

Let's get started.

Killing Processes Using CMD

To kill a process using the CMD, you need to know the Process ID (PID) then use the ID to kill that task. Let's see how to do that step by step.

  1. Open the Command Prompt by searching for it at the start menu.

Img 3.png

  1. Once open, find the process ID (PID) using the netstat command as shown below:

    >netstat -ano | findstr :<port_number>
    

    In place of port_number, use the port number that you want to free. For this demonstration, I want to free port 3000 as seen below.

    >netstat -ano | findstr :3000
    

    Output:

    Img 4.png

    From the command, we get our PID to be 3772 as seen on the right side of the image above. Your PID is likely to be different.

  2. Now that we have the PID, we can kill that process using the taskkill command. Here is the syntax.

    >taskkill /PID "Enter_PID" /F
    

    Replace Enter_PID with the process ID you got from the netstat command.

    >taskkill /PID 3772 /F
    

    Output:

    Img 5.png

    As you can see the process was terminated successfully.

  3. You can confirm this by running the netstat command again.

    >netstat -ano | findstr :3000
    

    You will notice that no processes are running on that port on localhost.

Kill Process Using Localhost Port Number

Instead of using the netstat command to find the PID then use the taskkill command, you can just specify the port number by using the npx kill-port command. Here is the syntax.

>npx kill-port <port_number>

Replace port_number with the port you want to free on localhost as shown below.

>npx kill-port 8000
NOTE: To use this command, you need to have the kill-port package installed. Otherwise, you will be prompted to install it as shown below.

Once the kill-port package is installed, the command will execute successfully.

Output:

Kill Process Using Powershell

Similar to Using the command prompt, you can use the Stop-Process and Get-Process commands to kill the process on localhost. Let's see it in action.

We first take note of the PID. We can get this with the netstat command.

> netstat -ano | findstr :3000

Output:

To kill the process, we use the stop-process command as shown below.

stop-process 38428

This command should kill the process running on port 3000 on the localhost.

Specifying the Port Number on Powershell

You can also specify the port number to avoid having to know the PID. To do this, we use the Get-Process command in conjunction with the stop-process command as shown below.

> Get-Process -Id (Get-NetTCPConnection -LocalPort "3000").OwningProcess | Stop-Process

The Get-Process command gets the port number which is then piped using the pipe operator | to the stop-process command to kill the process.

Killing the process using the Task Manager.

As usual, we must know the process ID before we can kill the tasks.

Use the netstat command to get the PID of the port running on localhost.

> netstat -ano | findstr :3000

Output:

Now that we have the PID, we can open up the task manager by pressing ctrl + shift + Esc keys. This will launch the task manager.

The next thing is to find the PID column if it is not visible. You can do this by clicking on the Details Tab.

This will add another column for PID as shown below.

Your work is to find the PID we got from the netstat command and kill the process.

To kill the process, scroll through the list of processes until you find the PID you are looking for. Once you find it, right-click on the process and select End task.

Conclusion

To sum up, we can use the npx kill-port command on cmd to kill a process that is using a port on localhost. A similar command for PowerShell is the Get-Process command piped with the stop-process command. This article shows how to use these commands in detail. These commands are very helpful especially when you need to use a specific port but the port is busy with another process. If you found the article helpful, check out our other tutorial on what is localhost. Thank you for reading : )

What is Localhost or 127.0.0.1?
In this tutorial, we will learn what the term localhost means, how it entails to the functionality of your devices, and some major uses of it.

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.