Summary: Guest blogger, Don Walker, explores using single and double quotation marks in Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. Welcome new guest blogger, Don Walker. Don is a coworker of guest blogger, Tim Bolton, and he worked with Tim on a project that Tim blogged about on June 18, 2015, Using PowerShell Grid View for User Info.
Here's Don's contact information:
Twitter: @EngineeringDon
LinkedIn: www.linkedin.com/in/EngineeringDon
A while back, I was asked to explain the difference between using single quotation marks ( ‘ ) and double quotation marks
( “ ) to define a string value, and when to use each. The feedback I received encouraged me to turn my explanation into an article so I could share it with others. My first thought was, “I don’t need to do that. How to define strings is too simple a topic.”
But thinking more about it, it is worth sharing. Understanding the difference between strings defined by single quotation marks or double quotation marks can save a huge amount of time and frustration. The two methods may initially look exactly the same, but the underlying action that occurs is very different.
Windows PowerShell can't recognize a string value unless delimiters (or boundaries) are defined by the user. In Windows PowerShell, single and double quotation marks are used to define the string. Essentially, “here is my start” and “here is my end.”
Here is an example of strings being defined and set to variables:
Notice that in the last example, the string is not contained in quotation marks so Windows PowerShell mistakenly confuses it for a command instead of a string value.
Single quotation marks define a literal string
Literal strings treat each character as a fixed value. What you see is what you get. Here is an example of how a string with single quotation marks ignores the variable character in Windows PowerShell ( $ ) and treats it as a regular dollar sign:
Using single quotation marks for basic strings is a best practice because there are no unexpected surprises with literal values. The only surprise that I have found has been attempting to use a single quotation mark character within a string that uses single quotation marks.
Windows PowerShell has a built-in remedy for this. Place an additional single quotation mark immediately after the first instance, for example:
Windows PowerShell also gives you the ability to define literal strings and concatenate them with variable values to form more complex strings, for example:
Double quotation marks define more dynamic parsing string
Parsing strings analyze each character within and parse certain characters with a special action, such as recognizing a variable and retrieving its value. This allows placing string values within a delimited string. Here is an example of using double quotation marks in a string to parse the value of a variable ($foo = ‘bar’) and place it within the string’s value:
This is the most common way to use double quotation marks in a string. As you get into loops and functions within your scripting, injecting changing variable values into static string output will keep your script’s output consistent. A real-world example of this could be a script that targets multiple servers and returns a string value if a certain process is running:
“${TargetServer} has ${process} running”
In addition to parsing variables, strings defined within double quotation marks also parse the backtick character ( ` ) to define escape characters and literal character values. Escape characters allow special actions to occur when processing the string, such as inserting a Tab space or starting a new line.
If a backtick is used, but it does not call an escape character, the character immediately after it will be a literal value and ignore any parsing rules. I recommend being familiar with the following backtick usage:
- `n– insert new line
- `t– insert horizontal tab
- `$– insert dollar sign instead of parsing a variable value
- `” – insert double quotation mark character instead of ending string
- ``– insert a single backtick character
Here is an example of the backtick being used as an escape character:
It is up to you as the Windows PowerShell user to decide which quotation mark style works best for your situation, but here are my final thoughts on the subject:
- Single quotation marks ( ‘ ) define a literal string. What you see is what you get.
- Strings that use single quotation marks are the preferred method.
- Double quotation marks ( “ ) define a dynamic, parsing string.
- Strings that use double quotation marks are most commonly used to invoke expressions or remote commands within a script.
- Strings begin and end with the same type of quotation mark (single or double).
- You should familiarize yourself with escape characters, if you haven’t already.
~Don
Thanks Don. This is a very helpful post.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy