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.
# A Basic String$myString = 'The price of a Beer is $6.00'Write-Output $myString
# Using String concatination$myItem = 'Beer'$myString = 'The price of a ' + $myItem + ' is $6.00'Write-Output $myString
# Using the format operator$myItem = 'Beer'$myPrice = '$6.00'$myString = 'The price of a {0} is {1}' -f ($myItem,$myPrice)Write-Output $myString
# Using Variables Inside A String with double quotes$myItem = 'Beer'$myString = "The price of a $myItem is $6.00"Write-Output $myString
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.
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 (`).
# Escaping the dollar sign using a back-tick$myItem = 'Beer'$myString = "The price of a $myItem is `$6.00"Write-Output $myString
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:
# Create a hash table for the item and use double quotes$myItem = @{ type = 'Beer' price = '$6.00'}
$myString = "The price of a $myItem.type is $myItem.price"Write-Output $myString
We run into another problem where - PowerShell isn’t extracting the value of the properties inside our hash table.
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:
# Create a hash table for the item and use double quotes$myItem = @{ type = 'Beer' price = '$6.00'}
$myString = "The price of a $($myItem.type) is $($myItem.price)"Write-Output $myString
What if we need to use single quotes inside our string?
# When our string contains single quotes
$myItem = @{ name = 'Matt' age = 29}
$myString = "The user '$($myItem.name)' is of age '$($myItem.age)'"Write-Output $myString
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.
# When our string contains double quotes
$myItem = @{ name = 'Matt' age = 29}
$myStringBackTicks = "The user `"$($myItem.name)`" is of age `"$($myItem.age)`""$myStringDoubleQuotes = "The user ""$($myItem.name)"" is of age ""$($myItem.age)"""Write-Output $myStringBackTicksWrite-Output $myStringDoubleQuotes
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.
$webData = @{ name = 'Matt' fontcolor = 'red' type = 'beer' price = '6.00' age = 29}
$htmlPage = "<html><head><title>$($name.title)'s Web Page!</title></head><body><{0}>Welcome to $($webData.name)'s Web Page!</{0}><p><font color=""$($webData.fontcolor)"">$($webData.name)'s Age is ""$($webData.age)"".</font><p>This means he can buy a $($webData.type) for `$$($webData.price)!</body></html>" -f 'h1'
Set-Content -Path C:\Temp\MyPage.html -Value $htmlPage
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.