Simplify PowerShell Output With the Expression CommandSimplify PowerShell Output With the Expression Command

Learn how the PowerShell Expression command simplifies the process of customizing output.

Brien Posey

March 7, 2025

3 Min Read
a powershell session screenshot under the word expression on a dark background

One PowerShell command I use with increasing frequency is the Expression command. Technically, it creates a calculated property. However, in practical terms, it makes it much easier to customize PowerShell's output to display precisely what you want.

The Problem: Extracting Process Names From TCP Connections

Here's an example: I set up several Hyper-V virtual machines to run a distributed application a few weeks ago. While doing so, I ran into networking problems related to a configuration error I had made. Troubleshooting involved using several different PowerShell cmdlets, but one of the most useful was Get-NetTCPConnection.

For those unfamiliar with Get-NetTCPConnection, it provides detailed information about a system's TCP connections. In my case, I needed to identify which process was using a given port to troubleshoot a connectivity issue.

I bring up this situation because while Get-NetTCPConnection can show you which process is using a given port number, it only displays the process ID (PID)—which, on its own, isn't very helpful. Yes, the PID corresponds to a specific process, but it's difficult to determine the process without knowing its name. Herein lies the problem: PowerShell does allow you to look up a process name based on a PID, but you need a separate command (Get-Process).

Related:Test-NetConnection Cmdlet: A PowerShell-Based Ping Alternative

Now, imagine you want to generate a list of local ports with the processes using them. Using conventional PowerShell scripting techniques, you would typically: 

  1. Use Get-NetTCPConnection to retrieve the raw port usage data and write it to an array. 

  2. Loop through each entry to exact the process ID. 

  3. Use Get-Process to look up the corresponding process name. 

  4. Create an array containing the port number and the process name before outputting the results. 

In short, while this method works, it requires quite a bit of effort. 

Simplifying With the Expression Command

This is where the Expression command simplifies things. You can accomplish the same task in a single line of code! Here's an example:

Get-NetTCPConnection | Select-Object LocalPort, OwningProcess, @{Name="ProcessName";Expression={(Get-Process -ID $_.OwningProcess).ProcessName}}

You can see what this looks like in Figure 1.

a screenshot of a PowerShell session demonstrates the information retrieved using the command

Figure 1. The command returns the information we need.

As shown in Figure 1, this command returns three key pieces of information:  

  • The local port number

  • The owning process (the process ID) 

  • The process name 

Let's look at what the command is doing.

Breaking Down What the Command Does

The first part is straightforward:

  • Get-NetTCPConnection retrieves active TCP connections. 

  • Select-Object specifies which properties to display, including the local port number and the owning process (PID).

Related:How I Built My Own PowerShell Multi-File Search Tool

Just after referencing the OwningProcess, @(Name="ProcessName" tells PowerShell to add a ProcessName column to the output. Since Get-NetTCPConenction doesn't return process names, we need to calculate it ourselves. 

The Expression command comes in now.

  • We use Get-Process to determine the process name based on the process ID stored within $_.OwningProcess

  • This process name appears within the custom ProcessName column.

Put simply, the Expression command acts as a bridge for passing a value to an unrelated command, retrieving a result, and displaying that result as a custom property in PowerShell's output.

For more PowerShell tips and tutorials, visit ITPro Today's YouTube channel.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like