0
0
PowerShellscripting~5 mins

Here-strings for multiline in PowerShell

Choose your learning style9 modes available
Introduction
Here-strings let you write multiple lines of text easily without using many quotes or special characters.
When you want to store a long message or text block in a variable.
When you need to create multiline strings for scripts or configuration files.
When you want to keep the text formatting exactly as you type it.
When you want to include quotes or special characters inside the text without escaping them.
Syntax
PowerShell
@"
Your multiline text goes here
"@
Start with @" on a new line and end with "@ on a new line.
Everything between these lines is part of the string, including line breaks and spaces.
Examples
Stores a simple multiline message in $text and prints it.
PowerShell
$text = @"
Hello,
This is a multiline string.
"@
Write-Output $text
Uses single-quoted here-string to keep text exactly as is, useful for JSON or code blocks.
PowerShell
$json = @'
{
  "name": "John",
  "age": 30
}
'@
Write-Output $json
Sample Program
This script saves a friendly message in a variable using a here-string and prints it exactly as typed.
PowerShell
$message = @"
Dear User,

Thank you for using our script.
Have a great day!
"@
Write-Output $message
OutputSuccess
Important Notes
Use double-quoted here-strings (@" "@) if you want to include variables inside the text and have them expand.
Use single-quoted here-strings (@' '@) if you want the text to be exactly as typed, without variable expansion.
Make sure the ending delimiter ("@ or '@) is on its own line, possibly preceded only by spaces or tabs.
Summary
Here-strings let you write multiline text easily in PowerShell.
They keep the text formatting and line breaks exactly as you type.
Use double or single quotes to control variable expansion inside the text.