Logo

dev-resources.site

for different kinds of informations.

How to Kill a Process Binding to a Specific Port in PowerShell

Published at
6/20/2024
Categories
terminal
powershell
process
port
Author
dutchskull
Categories
4 categories in total
terminal
open
powershell
open
process
open
port
open
Author
10 person written this
dutchskull
open
How to Kill a Process Binding to a Specific Port in PowerShell

If you frequently run into issues where a process is binding to a specific port and you need to free that port, PowerShell provides a convenient way to do this. In this blog post, weโ€™ll walk you through how to create a custom PowerShell function that kills the process binding to a specific port.

The Problem

Often, when working with web servers, databases, or other network services, you might encounter the "port already in use" error. This means that a process is currently using the port you are trying to bind your service to. Manually finding and killing this process can be tedious. Automating this task with PowerShell can save you time and hassle.

The Solution

PowerShell has cmdlets that can help you identify and stop processes. We will use Get-NetTCPConnection to find the process ID (PID) using the port and Stop-Process to terminate the process.

Step-by-Step Guide

1. Define the Function

We will create a function called Kill-PortProcess that takes a port number as a parameter and kills the process using that port.

function Kill-PortProcess {
    param (
        [int]$Port
    )
    try {
        $processId = (Get-NetTCPConnection -LocalPort $Port | Select-Object -ExpandProperty OwningProcess)
        if ($processId) {
            Stop-Process -Id $processId -Force
            Write-Host "Process with ID $processId that was using port $Port has been terminated."
        } else {
            Write-Host "No process is using port $Port."
        }
    } catch {
        Write-Host "An error occurred: $_"
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Save the Function

To make this function available in all your PowerShell sessions, save it in your PowerShell profile script. You can edit your profile script by running:

code $PROFILE
Enter fullscreen mode Exit fullscreen mode

Copy and paste the function definition into your profile script and save the file.

3. Use the Function

Now you can use this function in any PowerShell session. For example, to kill the process using port 8080, you would run:

Kill-PortProcess -Port 8080
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this custom PowerShell function, you can quickly and easily free up ports by terminating the processes that are using them. This can be particularly useful for developers and system administrators who frequently run into port conflicts.

Feel free to customize the function to better suit your needs, and happy scripting!


I hope this post helps you manage your ports more effectively. If you have any questions or suggestions, please leave a comment below.

process Article's
30 articles in total
Favicon
How Robotics Is Changing Material Handling in the Process Industry
Favicon
How Machine Learning Models are Shaping the Future of Process Automation
Favicon
๐Ÿš€ Visualize Success: BPMN is the Key to Efficient Business Process Management! Plus, a Masterclass on Using Miro Board! ๐ŸŒŸ๐Ÿ“ˆ๐ŸŽ‰
Favicon
Concurrency in Python with Threading and Multiprocessing
Favicon
Rubber Process Oil Market Opportunities, Challenges, Strategies and Forecast by 2031
Favicon
Understanding How Your OS Handles Print Requests to the Console ๐Ÿ–ฅ๏ธ
Favicon
What is Python GIL? How it works?
Favicon
[13/52] Fundamentals of Systems Engineering Models
Favicon
Insights into ITIL Processes
Favicon
Navigating Team Transitions: A Guide for Non-Technical Founders
Favicon
Elixir process - Explain more part2
Favicon
Elixir Supervisor, a powerful thing can help dev & devops sleep well!
Favicon
How to Kill a Process Binding to a Specific Port in PowerShell
Favicon
Elixir Process, what is? how work? What is linked & monitored process part1
Favicon
The 5 Stages Of My Software Development Process
Favicon
How does your company work with clients to understand theirย needs?
Favicon
How to Setup Incremental Design Process in a Startup
Favicon
Checklist for the non-tech founder
Favicon
Enhancing Business Efficiency: A Comprehensive Guide to Choosing and Implementing Process Mapping Software
Favicon
OS Fundamentals 101: Process and Syscalls
Favicon
Bashไธญๅญ่ฟ›็จ‹่งฃๆƒ‘
Favicon
Onboarding as a Senior Engineer
Favicon
Cons of the private chats for team collaboration
Favicon
A typical day at JetThoughts
Favicon
How to know what your team is doing?
Favicon
Why communication is so important when you work remotely?
Favicon
What activities are expected from a remote developer for effective collaboration
Favicon
Craft Visually Stunning Web Apps that Engage & Inspire Users
Favicon
Delegation is the last thing managers should do
Favicon
Meetings, Am I Right?

Featured ones: