Category Archives: Powershell

List Open Ports Using Powershell

In a previous post I showed how to use the netstat command to show open ports. This is another way using Powershell which gives you more options.

Get-NetTCPConnection is the Powershell equivalent to netstat and by itself will return a similar output to netstat.

Get-NetTCPConnection

To show only the listening ports we need to filter for all items in the Listen state with the remote address of 0.0.0.0

get-nettcpconnection | where {($_.State -eq "Listen") -and ($_.RemoteAddress -eq "0.0.0.0")}

You can add additional fields like the process ID for each port. Changing the fields from the default requires selecting each one you want and then piping to ft (format-table).

get-nettcpconnection | where {($_.State -eq "Listen") -and ($_.RemoteAddress -eq "0.0.0.0")} | Select LocalAddress,LocalPort,RemoteAddress,RemotePort,State,OwningProcess | ft

This example will get the name of the process associated with each item.

 get-nettcpconnection | where {($_.State -eq "Listen") -and ($_.RemoteAddress -eq "0.0.0.0")} | select LocalAddress,LocalPort,RemoteAddress,RemotePort,State,@{Name="Process";Expression={(Get-Process -Id $_.OwningProcess).ProcessName}} | ft