PowerShell has some of the best, most flexible string handling of any language I’ve used. In this post we’ll look at some string basics, then some of the features that make it special.
Note in these demos I’m using PowerShell Core 7.1.3 and VSCode. Everything here should also work in the PowerShell 5.1 and the PowerShell IDE.
Basic Strings in PowerShell
Strings in PowerShell can be denoted with either single quote marks or double quote marks.
"This is a string" 'This is a string too!'
If you want to see the result, simply highlight the strings and press either F8 in VSCode, or F5 in the PowerShell IDE.
This is a string This is a string too!
Multiline Strings, aka Here Strings
PowerShell has the ability to create a multiline string, also known as a “here” string. To create a here string, use an @ sign, followed by a quotation mark (either single or double) then hit Enter.
On the next lines, enter the value for your string. Finally hit Enter, then on the next line put the closing quotation mark then another @ sign.
This is important, the closing quotation mark and @ sign must be the first two characters on the line. If you attempt to indent them in any way the technique will not work!
$heretext = @" Some text here Some more here a bit more a blank line above "@ $heretext Some text here Some more here a bit more a blank line above
Again, this works with single or double quotes. So what’s a common use?
One common thing I use it for is SQL queries. Here’s an example in a traditional language.
$sql = 'SELECT col1' ` + ' , col2' ` + ' , col3' ` + ' FROM someTable ' ` + ' WHERE col1 = ''a value'' '
Note the single ` (back tick) mark at the end of each line. This is the line continuation character in PowerShell.
As you can see, each line has to be connected with + signs. Now let’s contrast it with a here string version.
$sql = @' SELECT col1 , col2 , col3 FROM someTable WHERE col1 = 'a value' '@
This is not only much more readable, but easier to work with. You can develop your SQL query in SQL Server Management Studio, or Azure Data Studio, then simply cut and paste it into PowerShell.
Another thing to notice, you can embed single or double quotes within a here string, and PowerShell will ignore them as denoting another string.
Embedded Quote Marks
If you need to embed a quotation mark in a string, you can simply double them up.
"ArcaneCode said ""PowerShell is awesome!"" and the crowd applauded." ArcaneCode said "PowerShell is awesome!" and the crowd applauded.
This technique works with single quote marks as well. For example one’s used in common words such as shouldn’t.
'You shouldn''t miss the blog posts at arcanecode.com' You shouldn't miss the blog posts at arcanecode.com
You may be wondering what the difference between single and double quotes is. Double quotes allow you to use something called string interpolation. String interpolation will take any variables embedded in the string and expand them before returning the string.
Here’s a simple example. First we get the number of items in the current folder. Then we copy the current folder location into another variable. Finally we create a string with these variables in it, surrounding the string in double quote marks.
$items = (Get-ChildItem).Count $loc = Get-Location "There are $items items are in the folder $loc." There are 3 items are in the folder C:\PSCore-QuickStart.
As you can see in the result (the bottom line) it has taken the value in
$items, a 3, and expanded it in the return result.
Likewise, it has taken the value in
$loc and expanded it to the name of the current folder.
Now see the result if single quote marks had been used.
$items = (Get-ChildItem).Count $loc = Get-Location 'There are $items items are in the folder $loc.' There are $items items are in the folder $loc.
With single quotes, the string interpolation engine does not kick in.
As a rule of thumb then, only use double quotes if you have to have interpolation. Otherwise use single quotes so you don’t incur the (admittedly minimal) amount of time for the string interpolation engine to needlessly process the string.
PowerShell also has the concept of escape characters. These are special characters you can embed in the string that provide additional formatting instructions.
Note that escape characters only work inside double quote marked strings. If you use single quotes for your strings, it will just display the escape character.
There are four that are the most commonly used. Tab, Carriage Return, Line Feed, and escaping the $ (dollar sign) so it may be used.
Escape characters are preceded by a back tick ` then the character. Let’s look at the tab, which is a `t.
"`tThis is tabbed in" This is tabbed in
The next is the carriage return line feed sequence, which is
"Here is some text`r`nAnd some more text`r`n`r`nA blank line before this" Here is some text And some more text A blank line before this
You would most commonly use this when looping over data and building a new output string.
The final escape character we’ll look at allows you to embed a $ character in an double quoted string. Simply use a back tick followed by the dollar sign.
"The `$items variable is $items" The $items variable is 3
We’ve only covered four here, but note there are many more escape sequences you can use in PowerShell.
In this post you saw all kinds of cool things you can do with PowerShell strings. With this knowledge you can take your PowerShell scripts to the next level, and be able to generate some creative output with your own PowerShell strings.
The demos in this course came from my Pluralsight course PowerShell 7 Quick Start for Developers on Linux, macOS and Windows, one of many PowerShell courses I have on Pluralsight. All of my courses are linked on my About Me page.
If you don’t have a Pluralsight subscription, just go to https://pluralsight.com. At the top is a Try For Free button you can use to get a free 10 day subscription to Pluralsight, with which you can watch my courses, or any other course on the site. Once there just search for my name, Robert Cain, to see all the courses I have.