Printing/Escaping a variable name in Powershell

In Powershell, if you include a variable name (like $PROFILE) in a statement that writes to the screen, it will be replaced with the contents of the variable:

PowerShell 7.4.1
PS C:\Users\Ryan> Write-Output "this is my profile: $PROFILE"
this is my profile: C:\Users\Ryan\OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

But what if you want to actually out the name of your variable instead of the contents? Like if you’re displaying an example statement to execute? In that case, you can escape it by using the backtick character (“`” – the backwards single-quote on in the upper left of your keyboard on the same key as the “~” tilde). In that case, your result will look like this:

PowerShell 7.4.1
PS C:\Users\Ryan> Write-Output "this is my profile: `$PROFILE"
this is my profile: $PROFILE

In my case, I was trying to print some instructions to the screen that included the $PROFILE, so that was the workaround.

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?