PowerShell Single vs Double Quotes

As a PowerShell developer, if you are working with quotes, you should know the difference between single and double quotes. In this tutorial, I will explain the difference between single and double quotes in PowerShell.

Single Quotes (') in PowerShell

Single quotes in PowerShell are used to define literal strings. When you enclose a string in single quotes, everything inside the quotes is treated as a literal value. This means that variables and special characters are not interpreted.

Syntax:

'This is a literal string'

Double Quotes (") in PowerShell

Double quotes, on the other hand, allow for string interpolation. This means that variables and expressions within the string are evaluated, and their values are inserted into the string.

Syntax:

"This string contains a variable: $variable"

Check out PowerShell Variables in Quotes

Difference Between Single and Double Quotes With Examples

Now, let me show you some examples to help you understand the differences between single and double quotes in PowerShell.

Example 1: Literal Strings with Single Quotes

Consider a scenario where you want to print a file path in a script. Using single quotes ensures that the backslashes are treated literally.

$path = 'C:\MyFolder\report.txt'
Write-Output $path

Output:

C:\MyFolder\report.txt

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

powershell single vs double quotes

Example 2: Variable Interpolation with Double Quotes

Now, let’s see how double quotes handle variables. Suppose we have a variable that holds a user’s name:

$name = "Alice"
$message = "Hello, $name! Welcome to the PowerShell tutorial."
Write-Output $message

Output:

Hello, Alice! Welcome to the PowerShell tutorial.

In this example, the variable $name is evaluated, and its value is inserted into the string.

Here is the output you can see in the screenshot below:

powershell difference between single and double quotes

Check out Add Double Quotes in a String in PowerShell

Example 3: Combine Strings

You can also combine strings using both single and double quotes. This is particularly useful when dealing with complex strings that contain both literal text and variables.

$city = "New York"
$info = 'The city of ' + $city + ' is known for its vibrant culture.'
Write-Output $info

Output:

The city of New York is known for its vibrant culture.

Example 4: Escaping Quotes

Sometimes, you may need to include quotes within your strings. PowerShell provides a way to escape quotes by doubling them.

Single Quotes:

$message = 'It''s a beautiful day in the neighborhood.'
Write-Output $message

Output:

It's a beautiful day in the neighborhood.

Double Quotes:

$quote = "He said, ""Hello, World!"""
Write-Output $quote

Output:

He said, "Hello, World!"

Example 5: Using Quotes in Command Parameters

Quotes can help ensure that spaces and special characters are handled correctly when passing parameters to commands.

$command = "Get-Process -Name 'notepad'"
Invoke-Expression $command

In this example, single quotes around notepad ensure that the process name is treated as a single argument.

Check out Escape Special Characters in Variables in PowerShell

PowerShell single vs double quotes

Here is a summary of the differences between single and double quotes in PowerShell.

FeatureSingle Quotes (')Double Quotes (")
String LiteralTreats everything as literalAllows variable interpolation
Variable EvaluationVariables are not evaluatedVariables are evaluated and their values inserted
Special CharactersSpecial characters are treated literallySpecial characters can be escaped or evaluated
Escaping QuotesDouble single quotes to include a single quoteDouble double quotes to include a double quote
Use Case Example'C:\Path\To\File'"The file path is $path"
Command ParametersUseful for passing exact string valuesUseful for combining strings and variables

Conclusion

I hope you now understand the differences between single and double quotes in PowerShell. Single quotes are ideal for literal strings where you don’t want any interpolation, while double quotes are perfect for strings that include variables and expressions.

Still, do you have any questions? Feel free to leave a comment below.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.