Posey's Tips & Tricks

How To Deal with Apostrophes in PowerShell Strings

In the last paragraph of my column "How To Debug a PowerShell Script," I mentioned that punctuation can be problematic in PowerShell strings. I often catch myself omitting apostrophes, for example, because including an apostrophe within a text string can make PowerShell misinterpret the string and cause problems.

To see what I mean, check out Figure 1. In the first line, I simply omitted the apostrophe from the word "I'm." In the second line I included the apostrophe, and PowerShell interpreted it as the end of the string.

Figure 1: PowerShell doesn't like apostrophes in strings.

Admittedly, PowerShell's seeming inability to handle apostrophes might not initially seem like a big deal. After all, most people probably won't care if your PowerShell strings are correctly punctuated or not.

However, there is a bigger issue to consider. PowerShell scripts are often used to generate HTML code. If you are using a PowerShell string to store HTML code, then there may not be any way to get around the need for apostrophes (or quotation marks, which can be just as problematic). Fortunately, there are several ways of dealing with this issue.

The first and probably most common method for dealing with apostrophes and quotation marks is using a technique of combining single and double quotes. Most people enclose PowerShell strings between apostrophes, but you can just as easily enclose a string inside quotation marks. Both are perfectly valid techniques. As such, if you know that you need to include an apostrophe within a string, then enclose the string with quotation marks. The figure below illustrates this concept in action.

[Click on image for larger view.] Figure 2: You can use quotation marks to enclose a string containing an apostrophe.

The second technique is sometimes referred to as escaping. The idea here is that PowerShell interprets certain character combinations as something other than just raw text. There are countless articles on how to use various escape codes in PowerShell, but to be perfectly honest, I haven't been able to make most of those techniques work properly. Even so, there is a technique I have found for using a special key combination to insert an apostrophe into a text string.

Rather than using a single apostrophe, type two apostrophes (make sure you don't put a space between them). PowerShell will see the two apostrophes and understand that you are trying to insert a single apostrophe into a text string. Here is an example.

$A = 'This technique won''t cause any problems'

As you can see in Figure 3, PowerShell interprets the double apostrophe as a single apostrophe. This technique works even though the string is enclosed in apostrophes.

Figure 3: PowerShell interprets a double apostrophe as a single apostrophe.

The third technique I want to show you is more suitable for dealing with large blocks of text where it would be impractical to address each apostrophe individually. This technique also works well in situations where using a combination of single quotes and double quotes isn't an option.

The idea behind this technique is that you can enclose a large block of text in quotation marks. PowerShell doesn't care what is in the text block (even if it includes apostrophes and quotation marks), so long as an "at" sign (@) is used at the beginning and end of the text block. It is worth noting that the markers indicating the beginning and end of the string must be on separate lines. Here is an example:

$A = @"
This is a "test"

You can see what this looks like in Figure 4.

Figure 4: This technique is useful if you need to create a large string that is likely to include a mixture of quotation marks and apostrophes, such as HTML code.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.


comments powered by Disqus

Subscribe on YouTube