My Profile Photo

Matthew Hodgkins Blog

❤️er of Platform Engineering, SRE, K8s, Golang, Observability and working with nice humans. Aussie living in the Netherlands.

The Ultimate Guide to PowerShell String Formatting

When scripting with PowerShell, you will come to a point where you need to work with strings that contain variables. Depending on your situation, there are several methods you can use when formatting strings in PowerShell. This blog will walk through these options.

We will start simple and ramp up the complexity.

We run into our first problem here. We are using a dollar sign in our string, so PowerShell thinks it is a variable and tries to insert it into a string.

Write-Output with Variable Doesn't Display

As we haven’t set a variable for $6, the string we get back is incorrect. To fix this problem, we need to escape the dollar sign so PowerShell leaves it alone. In PowerShell, escapes are done using a backtick (`).

Let’s create a hash table for our beer and try and use its properties in a string.

Once everything is installed, we will be customizing and setting up the following:

We run into another problem where – PowerShell isn’t extracting the value of the properties inside our hash table.

Write-Output Hashtable Variable Not Showing

We can wrap variables with properties inside some special tags to force PowerShell to return our variable inside the string, for example $($myItem.price). This is called a sub-expression:

What if we need to use single quotes inside our string?

This works fine, but if we wanted to use double quotes, we have 2 options. We can escape using a backtick (`) or escape using double-double quotes.

With the above knowledge we can handle all string formatting situations. Here a final complex example of creating a HTML page out of a PowerShell string. It includes single and double quotes, dollar signs and hash tables and uses the format command.

The beauty of PowerShell is that you can get the job done in multiple ways. You most likely would not mix all of them in a single script, but instead stick with the one you feel most comfortable with.

The above examples should make it a breeze to do even the most complicated string formatting. Happy PowerShell-ing!

Thanks to @Jaykul for reviewing this post.