Grab commandline of processes with Get-NetTCPConnection

You need to enclose the $_.owningprocess into a subexpression $()

Get-NetTCPConnection |
    Select-Object OwningProcess,
                 @{Name="cmdline";Expression={(Get-WmiObject Win32_Process -filter "ProcessId = $($_.OwningProcess)").commandline}}

Only the base variable can expand in double quotes without subexpression

And if you really want the last 7 processes you need to swap your Select and Format commands. As written 2 of the lines will be part of the formatting so you’ll end up with 5 processes.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top