Using only built-in commands and utilities, I have scripted WhatConnections.bat to display the Protocol, Local Address, Foreign Address, State, PID, Image Name, Session Name, and Session number for each connection on a computer.
The syntax for using WhatConnections.bat is WhatConnections.
The output is displayed on the console in the following CSV (Comma Separated Value) format:
"Proto","Local Address","Foreign Address","State","PID","Image Name","Session Name","Session Number"You can easily write the output to a file using:
You can process the output using:
set fnd1=FIND /I /V "Active Connections"
set fnd2=FIND /I /V "Proto Local Address"
for /f "Tokens=1-5" %%a in ('netstat -aon^|%fnd1%^|%fnd2%') do (
if "!pid!" EQU "" set pid=%%d&set state=NONE
for /f "Tokens=1-4 Delims=," %%i in ('TASKLIST /NH /FO CSV /FI "PID eq !pid!"') do (