Snippet: Netstat With Process Names

netstat.exe does not show process names, this always drives me crazy.

A function to correct this behavior can be found on my GitLab. Contributions are always welcome, if you want to improve the function!

It all boils down to a few lines of output manipulation if you want to quickly copy and paste this somewhere.

However the function has a bit more to offer, it can either show PID and process name (default) or switch out PID for process name.

$netstat = netstat -o
foreach ($result in $netstat) {
    $splitArray = $result -split " "
    $processId = $splitArray[$splitArray.length - 1]
    $processName = $processList | Where-Object {$ -eq $processID} | Select-Object -Property "processname"
    $splitArray[$splitArray.length - 1] = $transformedName
    $line = $splitArray -join " "
    Write-Host $line