Anthony Mastrean
Anthony Mastrean

Reputation: 22384

How do I replicate `netstat -b` with Get-NetTCPConnection?

I'm playing with Get-NetTCPConnection as a replacement for netstat. I need a solution for the -b flag, as the cmdlet only shows an OwningProcess PID.

-b Displays the executable involved in creating each connection or listening port.

https://www.computerhope.com/netstat.htm

Upvotes: 1

Views: 1201

Answers (1)

Moerwald
Moerwald

Reputation: 11254

I would go with:

Get-NetTCPConnection | select-Object LocalAddress, LocalPort,RemoteAddress,RemotePort,State , OwningProcess  , @{l="Name" ;e= {Get-Process -Id $_.OwningProcess | select -ExpandProperty Name } } | Format-Table

For easier usage, it could be wrapped to function:

Function MyNetStat {Get-NetTCPConnection | select LocalAddress, LocalPort,RemoteAddress,RemotePort,State , OwningProcess  , @{l="Name" ;e= {Get-Process -Id $_.OwningProcess | select -ExpandProperty Name } } }

Which could be added to your profile.ps1:

'Function MyNetStat {Get-NetTCPConnection | select LocalAddress, LocalPort,RemoteAddress,RemotePort,State , OwningProcess  , @{l="Name" ;e= {Get-Process -Id $_.OwningProcess | select -ExpandProperty Name } } }' | Out-File "$HOME\Documents\WindowsPowerShell\profile.ps1" -Append

Hope that helps.

Upvotes: 2

Related Questions