Create a Multiline or Formatted String
1. Problem
You want to create a variable that holds text with newlines or other explicit formatting.
2. Solution
Use a PowerShell here string to store and work with text that includes newlines and other formatting information.
       $myString = @"
       This is the first line
       of a very long string. A "here string"
       lets you to create blocks of text
       that span several lines.
       "@
3. Discussion
PowerShell begins a here string when it sees the characters @” followed by a newline. It ends the string when it sees the characters “@ on their own line. These seemingly odd restrictions allow you to create strings that include quote characters, newlines, and other symbols that you commonly use when you create large blocks of preformatted text.
Like string literals, here strings may be literal (and use single quotes) or expanding (and use double quotes).
In addition to their usefulness in preformatted text variables, here strings also provide a useful way to temporarily disable lines in your script. Since PowerShell does not provide a first-class multiline comment, you can use a here string as you would use a multiline comment in other scripting or programming languages. Example 5-1 demonstrates this technique.
Example 5-1. Using here strings for multiline comments
## This is a regular comment
$null = @"
function MyTest
{
  "This should not be considered a function"
}
Â
$myVariable = 10;
"@
Â
## This is regular script again
|
Using $null for the variable name tells PowerShell to not retain the information for your later use.
Tags: create string, format, multiline, script, variable
