Get-ListeningPrograms.ps1

<#PSScriptInfo
.VERSION 1.0.0
.GUID c4f81a27-5e6b-4d93-a0c8-71b2e9d36f04
.AUTHOR Coinetic AI Security - https://coinetic.io
.COMPANYNAME Coinetic AI Security
.COPYRIGHT (c) 2026 Coinetic AI Security - https://coinetic.io. MIT licence.
.TAGS Coinetic Windows WindowsServer Firewall Ports ListeningPorts OpenPorts Netstat Process
.LICENSEURI https://github.com/Coinetic-AI-Security/windows-firewall-manager/blob/main/LICENSE
.PROJECTURI https://github.com/Coinetic-AI-Security/windows-firewall-manager
.ICONURI https://coinetic.io/logo-192.png
.RELEASENOTES 1.0.0: which program listens on which TCP / UDP port, with the program path and whether it listens on all network cards or only locally.
#>


<#
.SYNOPSIS
    Shows which program listens on which TCP and UDP port - with the program's path and whether the port is open on
    all network cards or only on this computer.
 
    Free tool by Coinetic AI Security - https://coinetic.io
 
.DESCRIPTION
    Like "netstat -ano", but with the program name and path instead of only a process number, and without parsing
    any translated text: it uses the built-in Get-NetTCPConnection, Get-NetUDPEndpoint and Get-Process.
 
    "Listening" is not the same as "reachable from the internet": the Windows Firewall, your router or a cloud
    security group decide that. Use this list to see what COULD be reached, then check your firewall rules
    (Get-FirewallRuleReport.ps1, Find-RiskyFirewallRules.ps1).
 
    The script CHANGES NOTHING. Run it as administrator to see the program path of system services.
 
.PARAMETER Udp
    Also list UDP ports.
 
.PARAMETER AllAddressesOnly
    Only ports that listen on all network cards (0.0.0.0 / ::), not those bound to 127.0.0.1 / ::1.
 
.PARAMETER Quiet
    Do not print the table; only return the objects.
 
.EXAMPLE
    .\Get-ListeningPrograms.ps1
 
.EXAMPLE
    .\Get-ListeningPrograms.ps1 -Udp -AllAddressesOnly
 
.LINK
    https://coinetic.io
 
.LINK
    https://coinetic.io/manage-windows-firewall
#>

[CmdletBinding()]
param([switch]$Udp, [switch]$AllAddressesOnly, [switch]$Quiet)

Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'

if (-not (Get-Command Get-NetTCPConnection -ErrorAction SilentlyContinue)) {
    throw 'Get-NetTCPConnection is missing. It is built into Windows 8 / Windows Server 2012 and later.'
}

$procs = @{}
foreach ($p in @(Get-Process)) {
    $path = ''
    try { $path = $p.Path } catch { }   # access denied for some system processes without admin rights
    $procs[$p.Id] = [pscustomobject]@{ Name = $p.ProcessName; Path = $path }
}

function Get-Scope([string]$Address) {
    if ($Address -in @('0.0.0.0', '::')) { return 'all network cards' }
    if ($Address -in @('127.0.0.1', '::1')) { return 'this computer only' }
    return "only $Address"
}

$rows = New-Object System.Collections.Generic.List[object]
foreach ($c in @(Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue)) {
    $pr = $procs[[int]$c.OwningProcess]
    $rows.Add([pscustomobject]@{
        Protocol = 'TCP'; Port = [int]$c.LocalPort; Address = $c.LocalAddress; Scope = Get-Scope $c.LocalAddress
        Program = if ($pr) { $pr.Name } else { "(pid $($c.OwningProcess))" }; ProcessId = [int]$c.OwningProcess
        Path = if ($pr) { $pr.Path } else { '' }
    })
}
if ($Udp) {
    foreach ($e in @(Get-NetUDPEndpoint -ErrorAction SilentlyContinue)) {
        $pr = $procs[[int]$e.OwningProcess]
        $rows.Add([pscustomobject]@{
            Protocol = 'UDP'; Port = [int]$e.LocalPort; Address = $e.LocalAddress; Scope = Get-Scope $e.LocalAddress
            Program = if ($pr) { $pr.Name } else { "(pid $($e.OwningProcess))" }; ProcessId = [int]$e.OwningProcess
            Path = if ($pr) { $pr.Path } else { '' }
        })
    }
}
$list = @($rows | Where-Object { -not $AllAddressesOnly -or $_.Scope -eq 'all network cards' } |
          Sort-Object Protocol, Port, Address -Unique)

if ($Quiet) { return $list }
$list | Format-Table Protocol, Port, Scope, Program, ProcessId, Path -AutoSize -Wrap | Out-Host
Write-Host "$($list.Count) listening ports. Listening is not the same as reachable - the firewall decides that."
Write-Host 'Free tool by Coinetic AI Security - https://coinetic.io'