Home ยป PowerShell ยป Using PowerShell to Escape Double Quotes

Using PowerShell to Escape Double Quotes

Use the backtick character (`) in PowerShell to escape the double quotes in a double quotes string and use double quotes (โ€œ) to escape a single quote in PowerShell.

Quotation marks in PowerShell are used to enclose a string in either single quotation marks (โ€˜) or double quotation marks (โ€œ).

To prevent substitution of variable in double quotes string or to escape double quotes in PowerShell, use the backtick character.

In this article, we will discuss how to escape double quotes in a string and escape single quotes in PowerShell with examples.

Use Backtick Character to Escape Double Quotes in PowerShell

The backtick character (`) in PowerShell is used to escape double quotes in a string or prevent the substitution of a variable in double quotes string.

$str = "Welcome to `"ShellGeek`""
$str

In the above PowerShell script, the $str variable contains the string ShellGeek in double quotation marks in string.

To escape double quotes in a string, we have used the backtick character (`).

The output of the above PowerShell script after escaping the double quotes in a string prints the text that includes double quotes marks.

PowerShell Escape Double Quotes in String
PowerShell Escape Double Quotes in String

Escape Variable in Double Quotes in PowerShell

Use the backtick character (`) in PowerShell to prevent the substitution of a variable value in a double quotes string.

$count = 3 
# Use the backtick operator before variable to escape variable in double quoted string 
$result = "The current value of `$count is $count"
$result          

In the above PowerShell script, the $count variable contains a numeric value of 3.

To print the variable with its value enclosed in a double-quoted string, use the backtick character (`) before the variable. It will escape the variable in double-quoted string in PowerShell.

The output of the above script escape variable in double-quotes PowerShell string.

PS C:\> $count = 3                                                                                                      

PS C:\> $result = "The current value of `$count is $count"                                                              

PS C:\> $result                                                                                                         
The current value of $count is 3

PS C:\>                                                                                                                            

Use Double Quotes to Escape Single Quotes in PowerShell

Enclose a string in double quotes to escape the single quotes in PowerShell.

$str = "Welcome to 'ShellGeek'" 
$str

In the above PowerShell script, the $str variable contains the string. It has ShellGeek in single quotation mark.

To include single quotes in output or to escape single quotes in the string, enclose the string in double quotation marks (โ€œ).

The output of the above script in PowerShell escapes the single quotes in a string.

PS C:\> $str = "Welcome to 'ShellGeek'"                                                                                 

PS C:\> $str                                                                                                            
Welcome to 'ShellGeek'

Conclusion

I hope the above article helped you to understand how to escape double quotes in a string and escape single quotes in PowerShell.

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

Recommended Content

How to Create Multiple Strings in PowerShell