get-nettcpconnection | select local*,remote*,state,@{Name='Process';Expression={(Get-Process -Id $_.OwningProcess).ProcessName }} is listing the all process, I need to filter by process name.
31 Answer
if I understood the question correctly, you want to have only certain processes returned. you can do that with a where filter.
add the processes you want to have returned in $searchfor
$searchfor = @("wininit","TeamViewer") get-nettcpconnection | select local*,remote*,state,@{Name='Process';Expression={(Get-Process -Id $_.OwningProcess).ProcessName }} | where { $_.Process -in $searchfor } you could also do it like this, which is probably a bit faster, since you run get-nettcpconnection only for the processes you're interested in:
$searchfor = @("wininit","TeamViewer") Get-Process $searchfor | % { $Proc = $_ Get-nettcpconnection -OwningProcess $_.ID | select local*,remote*,state,@{Name='Process';Expression={$Proc.ProcessName}} }