Home » PowerShell Tips » PowerShell Multiline String

PowerShell Multiline String

While doing string operations in PowerShell, often we have to print multiline strings for ease to read, read file content and convert multiline string to array for string manipulation or write multiline string stored in a variable to file.

Use the following different ways to create PowerShell multiline string

  • PowerShell Carriage returns with a backtick character (`r`n)
  • Newline environment variable ([environment]::Newline) to create multiline string
  • PowerShell Here-String for multiple line string ( @” “@)
  • Line Break while defining string
PowerShell Multiline String
PowerShell Multiline String

In this article, I will explain to you different ways to create multiline string in PowerShell.

We will discuss different examples to convert multiline string to an array or store multiple lines in a file using PowerShell.

PowerShell Backtick Character ( `r`n)

You can use PowerShell special character like carriage return (r) and backtick character (`n) to create multiline string

Let’s consider an example to define multiline string using below script

# Using Backtick character for multiline string

"Welcome you to `r`nShellGeek"

To create multiline string, we have use `r`n , it will break the string to new line.

The output of above script as below

PS C:\> # Using Backtick character for multiline string

"Welcome you to `r`nShellGeek"

Welcome you to 
ShellGeek

PS C:\> 

Cool Tip: PowerShell String Concatenation with examples!

PowerShell Newline Environment Variable

Use the PowerShell Newline Environment variable ([environment]::Newline) to create multiline string.

For example, consider the string as “Welcome you to ShellGeek” and break the string into multiple line string, use the below script for line break

# Using Newline Environent variable for multiline string
"Welcome you to {0} ShellGeek" -f [environment]::NewLine

[environment]:: Newline environment variable will create multiline string as below

PS C:\> # Using Newline Environent variable for multiline string
"Welcome you to {0} ShellGeek" -f [environment]::NewLine
Welcome you to 
 ShellGeek

PS C:\> 

PowerShell Here-String to Define Multiline String

You can use the commonly-used PowerShell Here-String to create multiline string.

Here-String begins with @” and ends with “@ and is used to declare a block of text.

Let’s understand Here-String with an example to create multiple line string as below

# Using PowerShell Here-String for multiline string
@"
Welcome you to 
ShellGeek
"@

In the above script, PowerShell Here-String begins with @” at the beginning of the line and “@ at the end of the line. Between it contains the multiline string.

The output of the above PowerShell Here-String as below

PS C:\> # Using PowerShell Here-String for multiline string
@"
Welcome you to 
ShellGeek
"@

Welcome you to 
ShellGeek

PS C:\> 

Define String as Multiline String in PowerShell

You can create line break in string while defining it, the entire string must be enclosed within ” ” (double quotes)

For example, the below string will create output in multiline

# Create Multiline string while defining string

"Welcome you to
ShellGeek"

The output of the above script as below

PS C:\> # Create Multiline string while defining string

"Welcome you to
ShellGeek"
Welcome you to
ShellGeek

PS C:\> 

Cool Tip: How to create multiline comments in PowerShell!

PowerShell Multiline String with Double Quotes

Often string contains double quotes within it, if you want to create multiline string with double quotes, use PowerShell Here-String to do it easily.

#Using PowerShell Here-String for multiline string with double quotes
@"
Welcome you to
"ShellGeek"
"@

In the above PowerShell script, the string has double quotes for ShellGeek, we have used PowerShell Here-String to create multiline command.

The output of the above script as below

PS C:\> #Using PowerShell Here-String for multiline string with double quotes
@"
Welcome you to
"ShellGeek"
"@

Welcome you to
"ShellGeek"

PS C:\> 

Cool Tip: How to convert string to datetime in PowerShell!

PowerShell Multiline String with Single Quote

If you want to define multiline string with single quote, use PowerShell Here-String to define a string with a quote as below

#Using PowerShell Here-String for multiline string with single quotes
@"
Welcome you to
'ShellGeek'
"@

In the above PowerShell script, a string has a single quote for ShellGeek, we have used PowerShell Here-String to create multiline command.

The output of the above script as below

PS C:\> #Using PowerShell Here-String for multiline string with single quotes
@"
Welcome you to
'ShellGeek'
"@

Welcome you to
'ShellGeek'

PS C:\> 

Cool Tip: How to count lines in a file in PowerShell!

PowerShell Multiline String to Parameter

While doing string manipulation we have to store multiline string to parameter. You can use PowerShell Here-String to store multiline string to a parameter as below

#Using PowerShell Here-String for multiline string to Parameter

$test = @"
Welcome you to
ShellGeek 
"@

In the above PowerShell script, we have created a $test variable to hold multiline string created using PowerShell Here-String.

When we print $test, it prints multiple lines of string as below

PS C:\> #Using PowerShell Here-String for multiline string to Parameter

$test = @"
Welcome you to
ShellGeek 
"@

PS C:\> $test
Welcome you to
ShellGeek 

PS C:\> 

Cool Tip: How to use PowerShell tab character to insert a tab in a string!

PowerShell Multiline String to Array

Most use of multiline string while doing string manipulation after reading file, all data stored in multiline string in a variable.

If you need to perform any kind of string operation, convert multiline string to array and later perform the task on array elements.

For example, in the below script, we have a variable having a multiline string using PowerShell Here-String.

#Using PowerShell Here-String for multiline string to array

$test = @'
Welcome you to
ShellGeek
'@

$arrTest = $test.Split("`r`n")
Write-Host "Total Elements in array-->" $arrTest.Count

Write-Host "First element in array-->" $arrTest[0]

Write-Host "Second element in array-->" $arrTest[2]

In the above PowerShell script, we use split the function to a multiline string by `r`n . It returns an array of multiple lines and is stored in $arrTest an array variable.

Using Write-Host, we print the array count, the first element in the array, and so on of multiline string.

Cool Tip: How to create multiline command in PowerShell!

Conclusion

In the above article, we have learned how to create multiline string in PowerShell using different ways and store the multiline strings in a parameter or array.

PowerShell Here-String is the most commonly used technique to define a multiline string.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

Escape Double Quotes in PowerShell