NetStat – Get Network Statistics

| | | | | | | |
sudo lsof -i # Get all network traffic information
sudo lsof -i |grep *:http # Get all network traffic information using port 80 HTTP
netstat -tulpn # Get all network traffic information
netstat -tulpn | grep :80 # Get all network traffic information using port 80 HTTP
netstat -o # Get all network traffic information
Get-NetTCPConnection | `
	Where-Object {  `
		$_.State -eq "Listen" `
	} | `
	Sort-Object State, LocalPort | `
	Format-Table -AutoSize `
		LocalAddress, `
		LocalPort, `
		RemoteAddress, `
		RemotePort, `
		State, `
		@{l = "Process/Application"; e = { `
				$(Get-Process -PID $_.OwningProcess | Select-Object ID, ProcessName).ProcessName `
			} `
		}
function Get-NetStat 
    {
        $proc = @{};
        Get-Process | ForEach-Object { $proc.Add($_.Id, $_) };
        netstat -aon | Select-String "\s*([^\s]+)\s+([^\s]+):([^\s]+)\s+([^\s]+):([^\s]+)\s+([^\s]+)?\s+([^\s]+)" | ForEach-Object {
            $g = $_.Matches[0].Groups;
            New-Object PSObject | 
                Add-Member @{ Protocol =           $g[1].Value  } -PassThru |
                Add-Member @{ LocalAddress =       $g[2].Value  } -PassThru |
                Add-Member @{ LocalPort =     [int]$g[3].Value  } -PassThru |
                Add-Member @{ RemoteAddress =      $g[4].Value  } -PassThru |
                Add-Member @{ RemotePort =         $g[5].Value  } -PassThru |
                Add-Member @{ State =              $g[6].Value  } -PassThru |
                Add-Member @{ PID =           [int]$g[7].Value  } -PassThru |
                Add-Member @{ Process = $proc[[int]$g[7].Value] } -PassThru;
                #} | Format-Table Protocol,LocalAddress,LocalPort,RemoteAddress,RemotePort,State -GroupBy @{Name='Process';Expression={$p=$_.Process;@{$True=$p.ProcessName; $False=$p.MainModule.FileName}[$p.MainModule -eq $Null] + ' PID: ' + $p.Id}} -AutoSize
        } | Sort-Object PID | format-table
    }
Get-NetStat
Originally Posted on May 16, 2016
Last Updated on June 16, 2020
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.