Home Best Practice Strings Use Single Quotes By Default
 

Use Single Quotes By Default

In PowerShell, strings (text information) can be delimited by either single or double quotes. This is not equivalent. You should carefully consider whether you place text in single or double quotes.

Using Single Quotes By Default

Always make sure you use single quotes by default. Single quotes guarantee that the text will not be changed by PowerShell. Text in single quotes will appear exactly the way you defined it.

In the ISE editor, single-quoted text will by default be colored in brown:

  

'single-quoted text will not be tampered with by PowerShell'

When to use Double Quotes

You should use double-quoted text only when you explicitly want PowerShell to make changes to your text. There are good reasons to use double-quotes, but you should use double-quotes only if you really mean it.

Expanding Variables

One of the most popular use cases is inserting variable content into a string. Variables remain active inside of double-quoted text and will be replaced by their content. PowerShell colorizes variables in red, and variables inside a double-quoted string remain red. This is a good clue that they will be expanded:

  

$Name = 'Tobias Weltner'

# single-quoted text will not expand
# variables are colored brown
'Your name is $Name'

# double-quoted text will expand variables
# that is why variables remain colored red:
"Your name is $Name"

  Double-quoted text will keep variables active

Adding Escape Sequences

Another good reason for using double-quotes is to insert PowerShell escape sequences. Escape sequences always start with a backtick (`) and can add line feeds, tabulators, and beeping sounds (beeping works in the console only, not in the ISE editor):

  

# escape sequences are ignored in single-quoted text:
'this is a`nmultiline text`nHere is another line.'

# they do work in double-quoted text:
"this is a`nmultiline text`nHere is another line."

  Escape sequences work only in double-quoted text

And here is the result:

PS>
# escape sequences are ignored in single–quoted text:
'this is a`nmultiline text`nHere is another line.'

# they do work in double–quoted text:
"this is a`nmultiline text`nHere is another line."
this is a`nmultiline text`nHere is another line.
this is a
multiline text
Here is another line.

PS> 

Accidentally Using Double-Quoted Text

Carelessly using double-quoted text can yield unexpected results. You may accidentally expand variables that you were not aware of:

PS> "My IT budget is currently running low on $$$$$"
My IT budget is currently running low on clscls$

PS> 

  Accidentally expanding variables

In this example, the user was unaware that there is actually a variable called "$$" which happens to contain the last command token PowerShell executed. So the text is not as expected and also will change each time you run the code.

If you had used single-quotes right away, this problem would not have occured.

Always pay attention to the color coding applied by the ISE editor when you enter code. Even if you enter code in the ISE console pane, color coding would have told you that the variable "$$" was "live" in your double-quoted text:

PS> "My IT budget is currently running low on $$$$$"