Q. What is a here-string in Windows PowerShell?

Don Jones

May 24, 2010

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. Normally, a Windows PowerShell string is delimited by either single or double quotation marks:

$a = "String"
$b = 'String'

When you need to include a large block of strings that include single and double quotation marks, it can get complicated because you’re required to escape any quotation marks that are also being used to delimit the string:

$a = "This is my 'string,' and it contains `"quotes`"."

A here-string helps to avoid this by defining a block of text. You begin a here-string by using @" and end it by using "@.

There are some specific rules for where those starting and ending marks appear. You have to type it just as follows: The @" must be the last thing on the first line, then you type whatever arguments you want passed, and finally the closing "@ must be the first two characters on the next line:

$a = @"
This is a here-string. I can type "anything" I want,
even carriage returns, and it will all be preserved.
No need to escape!
"@
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like