Using Here-String Correctly

by Apr 30, 2013

Whenever you need to assign multi-line text to a variable, you can use so-called here-string. This is a sample:

$myCodeSnippet=@'
function Verb-Noun
{
  param()

}  
'@

Anything in between the quotes is untouched by the PowerShell parser and can contain any character (including special characters).

The price you pay for this is that here-string is picky. The opening tag must be at the end of a line (so add a line break right after it), and the closing tag must be at the very beginning of a line. If you accidentally indent the closing tag, the here-string will break.

This requirement makes sure that your here-string can contain almost anything without confusing the parser. The only thing you may not use in a here-string is the closing tag at line position 1.

Twitter This Tip! ReTweet this Tip!