Use Netstat to See Listening Ports and PID in Windows

by Guy McDowell

In another article, we explained computer ports and what they’re used for. Other than that, what can we do with port information? Since all traffic in and out of the computer goes through ports, we can check on them to see what they’re doing. Maybe the port isn’t listening for traffic? Maybe something is using a port that shouldn’t be? 

We’re going to use the Windows command netstat to see our listening ports and PID (Process ID). We’re also going to see what we can do with that information.

Table of Contents

    What Is Netstat?

    The netstat command is a combination of the words ‘network’ and ‘statistics’. The netstat command works in all versions of Windows from Windows XP right up to Windows 10. It’s also used in other operating systems (OS) like Unix and Linux, but we’ll stick to Windows here.

    Netstat can provide us with:

    Using Netstat To See Listening Ports & PID

    What’s Using That Port?

    How To Get Port, PID, & Process Name In PowerShell

    PowerShell is Microsoft’s newer way to use a command-line interface with Windows. We say newer, but it’s been around for several versions. You should learn PowerShell even if you’re a home user.

    Most Windows commands also work in PowerShell, plus we can combine them with PowerShell’s cmdlets – pronounced command-lets. Joe at Winteltools.com provides the script for this method.

    $netstat = netstat -aon | Select-String -pattern "(TCP|UDP)"
    $processList = Get-Process
    
    foreach ($result in $netstat) {
       $splitArray = $result -split " "
       $procID = $splitArray[$splitArray.length – 1]
       $processName = $processList | Where-Object {$_.id -eq $procID} |    select processname
       $splitArray[$splitArray.length – 1] = $procID + " " +      $processName.processname
       $splitArray -join " "
    }

    Go Get Them

    We’ve covered two ways to use the netstat command to see listening ports. It can be used either in the old Command Prompt or within a PowerShell script. With the information it can give us, we’ve looked at how it can help us figure out what our computer is doing. 

    If you thought netstat is a great utility, take a look at some other Windows TCP/IP utilities like tracert, ipconfig, and nslookup. Or use Resource Monitor to get a better look into hidden website and Internet connections. There is a lot you can do to see exactly what your computer is doing.

    Have you used netstat to solve a problem? Please tell us what you did. Any questions about how to use netstat? Please ask us in the comments below.

    Exit mobile version