Skip to content

How to use PowerShell subexpressions within strings

Black and White PDQ logo
Kris Powell|Updated December 21, 2020
Generic blog header
Generic blog header

Much like a poorly crafted sandwich, have you ever tried to access properties of an object from within a string only to have it fall apart?

Good news, everybody! Subexpressions are the answer! (Not to your sandwich woes, though. You’ll still need to work on that one).

Did you know that you can do subexpressions within your strings? *le gasp!* Did you also know that you can use subexpressions to access object properties within strings? *le double gasp!*

You can use the subexpression operator $() to do some awesome things within strings. For more information on PowerShell operators, check out this link.

Subexpressions

First and foremost, what in the world is a subexpression? Or even an expression for that matter?!

Simply put, an expression is a statement that can be evaluated and gives a result.

What is regular expression in PowerShell?

Glad you asked. Here's how to use regular expression, or regex.

A subexpression is an expression within an expression…think Inception.

Let’s do some math to demo how to use subexpressions. PowerShell can already do math pretty easily, so we’ll do some basic addition.

2+2

Blog-Subexpressions-2-plus-2

Awesome.

Yay math. If we try using that within a string, however, it ends up printing it instead of evaluating it.

Blog-Subexpressions-Write-output-2-plus-2

Now, let’s use the subexpression operator to show you how to perform all the maths within a string. This will evaluate the expression 2 + 2 within the double quotes.

Write-Output "$(2+2)" Write-Output "2 + 2 = $(2+2)"
Blog-Subexpressions-2-plus-2-3

You can see that we can seamlessly combine our text and our subexpression to get the output that we’re looking for!

That’s PowerShell magic at work right there.

Now, what about the promise of using subexpressions to access object properties?! Coming right up!

Using subexpressions within strings to access object properties

We can extend the use of subexpressions to access object properties.

First, let’s start our example by showing what many people are tempted to do within strings.

Let’s say that we want to show the Id for our PowerShell process within our output. (Get-Process powershell)

$MyPowerShellProcess = Get-Process powershell $MyPowershellProcess.Id

Easy peasy, right? Based off of this result, you might be tempted to use the following code to output the process id within a string:

$MyPowerShellProcess = Get-Process powershell Write-Output "My PowerShell process ID is: $MyPowershellProcess.Id"
Blog-Subexpressions-You-might-be-tempted-Example-1

Sadly, you’ll notice this isn’t the output you expected. You’ll need to access the property from a subexpression.

Here’s how you can use subexpressions to get the object properties.

$MyPowershellProcess = Get-Process powershell Write-Output "My PowerShell process ID is: $($MyPowershellProcess.Id)"
Blog-Subexpressions-Subexpression-with-PowerShell-Id

This is fantastic! We could even take this a step further and gather all this info from within the subexpression itself.

Write-Output "My PowerShell process ID is: $(Get-Process powershell | Select -ExpandProperty Id)"
Blog-Subexpressions-Subexpression-with-PowerShell-Id-one-line

Behold the power!

Using PowerShell to search through files for matching strings

Here's how to use the Select-String cmdlet to find what you're looking for.

Wrapping up

Yes, it’s true. Gone are the days of simply assigning values to variables like animals. Embrace the power of the subexpression!

…Just kidding.All I ask is that you continue making your code easier to read and use variables too. Pretty please.

Happy PowerShelling!

PS: There are other ways to access that information within a string, such as assigning the value directly to a variable or using the format operator. But, we’ll save that for another day.

To see this in action, please watch this awesome video:

Loading...

Black and White PDQ logo
Kris Powell

Kris was an employee at PDQ.

Related articles