Double quote in PowerShell – backtick (`) & here-string approach


Favourite is to use the double quotation marks to create an expanding string. I then escape the required internal quotation marks by using the backtick (`) character, as shown here:

PS C:\> $c = "My name is `"$env:UserName`""
PS C:\> $c
My name is "ed"
PS C:\>

Another way to get double quotation marks into the string is to use a here-string. Just like regular strings, there are the literal and the expanding variety. The advantage of a here-string is that it ignores things such as quotation marks and other special characters. Essentially, what you type in a here-string is exactly what you get. But with the expanding variety of here-string, you also have the opportunity to bring in the value of variables. This is shown here:

PS C:\> $c = @"
>> My name is "$($env:UserName)"
>> "@
>>
PS C:\> $c
My name is "ed"
PS C:\>