In PowerShell, there are several ways to escape the dollar sign ($) in a string, including using the backtick character (`) or enclosing the string in single quotes.
The following methods show how you can do it with syntax.
Method 1: Using backticks to escape $ in string
$string = "The total price is `$1000."
Write-Output $string
This example will use the backtick character (`) to escape dollar sign ($) in a string.
Method 2: Using single quotes
$string = 'The total price is $1000.'
Write-Output $string
In this example, the string is enclosed in a single quote and does not need to escape the dollar sign ($) in a string.
The following examples show how you can use these methods.
Using Backticks to Escape Dollar Sign ($) in String
The backtick character (`) can be used to escape the dollar sign ($) in a string.
# define a string that includes dollar sign ($) within it $string = "The total price is `$1000." # Outputs the string Write-Output $string
Output:
The total price is $1000.
In this example, the $string variable contains a string enclosed in double quotes that includes the dollar sign ($).
To escape the dollar sign ($) in a string, you can use the backtick character (`) before the dollar sign.
Finally, the Write-Output cmdlet outputs the string.
Using Single Quotes to Escape $ sign
The single quotes in PowerShell treat everything inside as a literal string, hence we don’t need to escape the dollar sign ($).
# define a string enclosed in single quotes and includes dollar sign $string = 'The total price is $1000.' Write-Output $string
Output:
The total price is $1000.
In this example, the $string variable contains a string enclosed in a single quote and includes the dollar sign ($) within it.
In PowerShell, everything inside the single quote is treated as a literal string.
Finally, the Write-Output cmdlet outputs the string.
Conclusion
I hope the above article on escaping the dollar sign ($) in PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.