How to Find Ports in Use using PowerShell?

Recently, while troubleshooting network connectivity issues, I needed to know which ports are in use. You can easily do this using Microsoft PowerShell. In this tutorial, I will explain to you how to find ports in use using PowerShell using various methods.

What Are Ports?

A port is a communication endpoint used by networked applications to exchange data. Each port is associated with a port number ranging from 0 to 65535. For example:

  • Port 80 – HTTP (web traffic)
  • Port 443 – HTTPS (secure web traffic)
  • Port 25 – SMTP (email)
  • Port 3389 – RDP (Remote Desktop)

When multiple applications try to use the same port, conflicts occur. This can cause services to fail or behave unpredictably. Therefore, knowing which ports are in use is essential for troubleshooting and security auditing.

Check ut Enable BitLocker with PowerShell

Method 1: Using Get-NetTCPConnection

Starting with Windows 8 / Windows Server 2012, Microsoft introduced the Get-NetTCPConnection cmdlet, which provides a modern, PowerShell-native way to view active TCP connections.

Example:

Get-NetTCPConnection

This command lists all active TCP connections, including:

  • LocalAddress – The IP address on your system.
  • LocalPort – The port number in use.
  • RemoteAddress – The connected remote IP.
  • RemotePort – The port number of the remote host.
  • State – The connection status (e.g., Established, Listen, TimeWait, etc.).
  • OwningProcess – The process ID (PID) using the port.

Here is the exact output in the screenshot below:

Find Ports in Use using PowerShell

Filtering for Listening Ports

To display only ports currently listening for incoming connections:

Get-NetTCPConnection | Where-Object { $_.State -eq 'Listen' }

Example Output:

LocalAddress LocalPort State OwningProcess
------------- -------- ----- -------------
0.0.0.0       80       Listen  1234
0.0.0.0       443      Listen  4567

This tells you that ports 80 and 443 are open and waiting for connections.

You can see the exact output in the screenshot below:

Find Ports in Use using PowerShell

Check out Find SQL Server Instances using PowerShell

Method 2: Finding Ports by Process ID (PID)

Once you identify a port in use, you may want to know which application is using it. You can map the OwningProcess (PID) to a process name using Get-Process.

Example:

Here is an example.

Get-NetTCPConnection | Select-Object LocalPort, State, OwningProcess

Then, find the corresponding process:

Get-Process -Id 1234

This command will reveal details such as the process name, path, and memory usage.

You can combine both commands into a single pipeline:

Get-NetTCPConnection | 
Where-Object { $_.State -eq 'Listen' } |
Select-Object LocalPort, @{Name='ProcessName';Expression={(Get-Process -Id $_.OwningProcess).ProcessName}}

This displays a clean table showing which ports are listening and which processes own them.

Check out Find OU of a Computer Using PowerShell

Method 3: Using Test-NetConnection to Check Specific Ports

If you only need to check whether a specific port is open or reachable (locally or remotely), use the Test-NetConnection cmdlet.

Example: Test if Port 80 is open on localhost

Here is a PowerShell script to test if port 80 is open on the localhost.

Test-NetConnection -ComputerName localhost -Port 80

Example: Test a remote server

Test-NetConnection -ComputerName www.google.com -Port 443

This cmdlet tests connectivity and returns:

  • TcpTestSucceeded (True/False)
  • RemoteAddress
  • PingSucceeded
  • RoundTripTime

This is especially useful for verifying firewall rules or checking if a service is accessible.

Here is the exact output shown in the screenshot below.

PowerShell Find Ports in Use

Read RPC Server is Unavailable Error in PowerShell

Method 4: Using netstat within PowerShell

If you prefer traditional tools, you can run netstat directly from PowerShell. Netstat provides detailed information about active connections and listening ports.

Example:

Here is an example of using the netstat PowerShell cmdlet.

netstat -aon

Explanation of switches:

  • -a — Displays all connections and listening ports.
  • -o — Shows the owning process ID.
  • -n — Displays addresses and ports in numeric form.

To find a specific port, you can filter with findstr:

netstat -aon | findstr :80

Then, use PowerShell to identify the process:

Get-Process -Id <PID>

This hybrid approach is handy when working across older Windows versions that lack newer PowerShell cmdlets.

Check out PowerShell Send Email to Multiple Recipients

Method 5: Using PowerShell to Check UDP Ports

While Get-NetTCPConnection handles TCP, you can use Get-NetUDPEndpoint for UDP ports.

Example:

Get-NetUDPEndpoint

Filter by Specific Port:

Get-NetUDPEndpoint | Where-Object { $_.LocalPort -eq 53 }

This is useful for identifying services like DNS (port 53) or other UDP-based applications.

Method 6: Closing or Freeing a Port

Once you identify a port that’s blocking an application, you can stop the process using it.

Example:

Stop-Process -Id <PID> -Force

⚠️ Caution: Stopping a process may interrupt critical services. Always verify the process name before terminating it.

You can verify again that the port is free:

Get-NetTCPConnection | Where-Object { $_.LocalPort -eq 80 }

If no output appears, the port is no longer in use.

Check out Get .NET Version Using PowerShell

Method 7: Exporting Port Information for Auditing

For auditing, reporting, or compliance purposes, it’s often useful to export the list of open or listening ports to a file such as CSV. This allows you to analyze the data later or share it with your team.

Example: Export All TCP Connections to CSV

Get-NetTCPConnection | 
    Select-Object LocalAddress, LocalPort, State, OwningProcess |
    Export-Csv -Path "C:\Reports\OpenPorts.csv" -NoTypeInformation

This command creates a CSV file named OpenPorts.csv in the C:\Reports directory. You can open this file in Excel or any text editor to review port usage.

Including Process Names in Export

To make the report more readable, include the process name by expanding the pipeline:

Get-NetTCPConnection | 
    Where-Object { $_.State -eq 'Listen' } |
    Select-Object LocalAddress, LocalPort, State, OwningProcess,
        @{Name='ProcessName';Expression={(Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue).ProcessName}} |
    Export-Csv -Path "C:\Reports\ListeningPorts.csv" -NoTypeInformation

This enhanced report shows which processes are listening on which ports, making it easier to identify services.

Check out How to Run PowerShell as Different User?

Troubleshooting Tips

Scenario: Port 80 Is Already in Use

If you try to start a web server (IIS, Apache, Nginx) and get an error that port 80 is in use, run the script below:

Get-NetTCPConnection -LocalPort 80 | Select-Object LocalAddress, State, OwningProcess

Identify the process:

Get-Process -Id <PID>

If it’s a non-essential process, stop it:

Stop-Process -Id <PID> -Force

Scenario 2: Unable to Find Process for a Port

Sometimes, the process may have exited, but the port still appears busy due to TIME_WAIT or other TCP states. These are temporary and usually clear after a timeout.

Conclusion

In this tutorial, I explained how to get ports in use using PowerShell, with various methods.

  • Use Get-NetTCPConnection to find active TCP ports and their owning processes.
  • Filter by listening state to identify open ports waiting for connections.
  • Use Get-Process to map process IDs to application names.
  • Test specific ports with Test-NetConnection.
  • Use netstat as a fallback on older systems.
  • Export port data to CSV for auditing and reporting.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.