Home » How to Escape Double Quotes in String in PowerShell

How to Escape Double Quotes in String in PowerShell

In PowerShell, handling double quotes in a string can be necessary when the string will be used in the context where double quotes need to be treated as literal.

The following methods show how you can do it with syntax.

Method 1: Using backticks to escape double quotes

$string = "This is `"double-quoted`" string example"

Write-Output $string

This example uses the backticks character (`) to escape double quotes in the double-quoted string.

Method 2: Using single quotes

$string = 'This is "single-quoted" string example'

Write-Output $string

This example uses single quotes to define the string, it allows double quotes to be used inside without escaping.

Method 3: Using doubling the double quotes

$string = "This is ""double-quoted"" string example"

Write-Output $string

In this example, it doubles the double quotes within the string to escape them.

Method 4: Using Here-String

$string = @" 
Tom said, "it was superb experience!" 
This is another line. 
"@

Write-Output $string

This example uses Here-String to handle double quotes without the need to escape them.

The following examples show how you can use these methods in practical application.

Using Backticks to Escape Double Quotes in a String

The backticks (`) can be used to escape double quotes within a string. Here’s how you can do it.

# define the string that includes double quotes
$string = "This is `"double-quoted`" string example."

# Output the string
Write-Output $string

Output:

This is "double-quoted" string example.

In this example, the $string variable contains the string enclosed in double quotes and includes the double quotes within a string.

We then use the backticks (`) character before the double quotes within a string to escape them.

Finally, the Write-Output cmdlet outputs the string.

Using Single Quotes to Handle Double Quotes in a String

The simplest way to handle double quotes within a string is to use the single quotes to enclose the string. Here’s how you can do it.

# define a string enclosed in a single quotes that contains double quotes within it.
$string = 'This is "single-quoted" string example.'

# Output the string
Write-Output $string

Output:

This is "single-quoted" string example.

In this example, the $string variable contains the string enclosed in a single quote and includes double quotes within a string.

You can use double quotes directly within a string without escaping them.

Finally, the Write-Output cmdlet outputs the string.

Using Doubling the Double Quotes in a String

You can double the double quotes within the string to escape them when the string is enclosed in the double quotes.

# define the string
$string = "This is ""double-quoted"" string example."

# Output the string
Write-Output $string

Output:

This is "double-quoted" string example.

In this example, we double the double quotes within a string to escape the double quotes.

Finally, the Write-Output cmdlet outputs the string.

Using Here-String to Handle Double Quotes

You can use the Here-Sting that can handle multi-line strings and strings with double quotes without needing to escape them.

$string = @" 
Tom said, "it was superb experience!" 
Let's enjoy.
"@

Write-Output $string

Output:

Tom said, "it was superb experience!"
Let's enjoy.

In this example, we define a here-string with double quotes and assign it to the variable $string.

The Write-Output cmdlet outputs the string.

Conclusion

I hope the above article on escaping double quotes in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.