JSI Tip 10628. How can I list the processes that use a specified TCP or UDP port?

Jerold Schulman

July 9, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today


I have scripted PortProcess.bat to list the processes that use a TCP or UDP port that you specify.

The syntax for using PortProcess.bat is:

PortProcess Port

where Port is the TCP or UDP port number, like 22 or 445 or 2000 or 42508.

PortProcess.bat uses Netstat.exe from Windows NT 5.1, and greater, and PSList.exe, both of which must be located in a folder that is in your PATH.

PortProcess.bat contains:

@echo offif "%1"=="" @echo Syntax: PortProcess PortNumber&goto :eofsetlocal ENABLEDELAYEDEXPANSIONset Port=%1set port=%port: =%set blank=       #set blank=%blank:~0,6%if exist "%TEMP%PortProcess.tm1" del /q "%TEMP%PortProcess.tm1"for /F "tokens=*" %%x in ('netstat -ano^|find ":%Port% "') do ( set line=%%x call :Sub)if not exist "%TEMP%PortProcess.tm1" goto Finishsort "%TEMP%PortProcess.tm1" /O "%TEMP%PortProcess.tm2"del /q "%TEMP%PortProcess.tm1"set prev="NONE"for /f "TOKENS=*" %%p in ('type "%TEMP%PortProcess.tm2"') do ( if !PREV! NEQ "%%p" set PREV="%%p"&@echo %%p)del /q "%TEMP%PortProcess.tm2":finishendlocalgoto :eof:Subset pid=%line:~69,5%for /f "Skip=8 Tokens=*" %%a in ('pslist %pid%') do ( set work=%%a set pn=!work:~0,17!# set pn=!pn:  =! set pn=!pn: #=! set pn=!pn:#=! @echo Port %port% is used by !pn!.>>"%TEMP%PortProcess.tm1")



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like