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

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 off
if "%1"=="" @echo Syntax: PortProcess PortNumber&goto :eof
set Port=%1
set 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 Finish
sort "%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"
goto :eof
set 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"

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.