How to Add a New Line in a PowerShell String

To add a new line in a PowerShell string, put `n (backtick n) inside a double-quoted string. The text after it starts on the next line:

$message = "Order 1042 has shipped.`nIt should arrive by Friday, October 2, 2026."
$message

Output:

Order 1042 has shipped.
It should arrive by Friday, October 2, 2026.
PowerShell new line in a string with backtick n
A backtick n splits the string over two lines (PowerShell 7)

That covers most scripts. Below I show the other new line options, when each one matters, and how to turn spaces into new lines.

I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, and the results were identical.

`n vs `r`n vs [Environment]::NewLine

PowerShell has three common ways to write a line break. This shows the actual characters each one produces on Windows:

function Show-Breaks([string]$Text) { $Text.Replace("`r", '\r').Replace("`n", '\n') }

'Backtick n:           ' + (Show-Breaks "A`nB")
'Backtick r n:         ' + (Show-Breaks "A`r`nB")
'Environment NewLine:  ' + (Show-Breaks "A$([Environment]::NewLine)B")

Output:

Backtick n:           A\nB
Backtick r n:         A\r\nB
Environment NewLine:  A\r\nB
SyntaxCharactersUse it for
`nLine feed (LF)Console output and most strings
`r`nCarriage return + line feed (CRLF)Windows text files and tools that expect CRLF
[Environment]::NewLineCRLF on Windows, LF on Linux and macOSScripts that run on more than one OS

In the console, `n and `r`n look the same. The difference only shows up in files and in programs that read them.

Why `n doesn’t work in single quotes

Backtick escapes only work in double-quoted strings. Single quotes print everything exactly as typed:

'Single quotes: Line one`nLine two'
"Double quotes: Line one`nLine two"

Output:

Single quotes: Line one`nLine two
Double quotes: Line one
Line two

If your `n shows up as literal text, check the quotes first. Microsoft lists every escape sequence in about_Special_Characters.

Use [Environment]::NewLine for cross-platform scripts

[Environment]::NewLine returns the line break of the OS the script runs on. It’s handy when you build a string by adding parts together:

$address = '1600 Pennsylvania Ave NW' + [Environment]::NewLine + 'Washington, DC 20500'
$address

Output:

1600 Pennsylvania Ave NW
Washington, DC 20500

PowerShell 7 runs on Linux and macOS too, so this keeps your output right everywhere without checking the OS yourself.

Write multi-line text with a here-string

For longer text, like an email body, a here-string is easier to read than a string full of `n. Everything between @" and "@ keeps its line breaks, and variables still expand:

$name  = 'Alice'
$total = 149

$body = @"
Hi $name,

Your order total is `$$total.
Thanks for shopping with us.
"@

$body

Output:

Hi Alice,

Your order total is $149.
Thanks for shopping with us.

The closing "@ must start at the beginning of its own line. Use @' and '@ instead when you don’t want variables to expand. about_Quoting_Rules covers both forms.

One thing to know: a here-string gets its line breaks from the script file itself. I saved the same script with CRLF and LF endings, and the here-string had CRLF in one and LF in the other.

Join lines into one string

If your lines are already in an array, -join puts a new line between them:

$report = 'Server: WEB01', 'Status: Running', 'Uptime: 12 days'
$report -join [Environment]::NewLine

Output:

Server: WEB01
Status: Running
Uptime: 12 days

For console output only, Write-Host can do the same with -Separator:

Write-Host 'Backup started' 'Copied 12 files' 'Backup finished' -Separator "`n"

Output:

Backup started
Copied 12 files
Backup finished

See how to concatenate strings in PowerShell for more ways to combine text.

Convert spaces to new lines

To put each word of a string on its own line, replace the spaces with `n. The .Replace() method is the simplest way:

$fruits = 'Apple Banana Cherry Mango'
$fruits.Replace(' ', "`n")

Output:

Apple
Banana
Cherry
Mango
PowerShell convert spaces to new lines with Replace
Each word on its own line (Windows PowerShell 5.1)

Handle tabs and double spaces

.Replace() only matches one exact space. The -replace operator takes a regular expression, so \s+ matches any run of spaces and tabs:

$servers = "WEB01  WEB02`tSQL01 `t FILE01"
$servers -replace '\s+', "`n"

Output:

WEB01
WEB02
SQL01
FILE01

Split, clean up, then join

With extra spaces, splitting on a single space leaves empty items. Trim the string and split on \s+ instead, then join with a new line:

$servers = '  WEB01  WEB02   SQL01  '

"Split on one space:  $(($servers -split ' ').Count) items"
"Split on whitespace: $(($servers.Trim() -split '\s+').Count) items"

($servers.Trim() -split '\s+') -join "`n"

Output:

Split on one space:  10 items
Split on whitespace: 3 items
WEB01
WEB02
SQL01

This way you get an array in the middle, so you can filter or sort the items before joining. Splitting strings in PowerShell goes deeper.

Turn a space-separated file into one item per line

Say servers.txt holds WEB01 WEB02 SQL01 FILE01 PRINT01 on one line. Read it, split it, and write one name per line:

$names = (Get-Content -Path C:\psfaqs\servers.txt -Raw).Trim() -split '\s+'
$names | Set-Content -Path C:\psfaqs\server-list.txt

Get-Content -Path C:\psfaqs\server-list.txt
"Lines in the new file: $((Get-Content -Path C:\psfaqs\server-list.txt).Count)"

Output:

WEB01
WEB02
SQL01
FILE01
PRINT01
Lines in the new file: 5
PowerShell split space-separated text in a file into one item per line
One server name per line in the new file (PowerShell 7)

Set-Content writes each array item on its own line, so you don’t need to add any new line characters yourself.

Split a string into lines

To go the other way, split on "`r?`n". It matches both Windows and Linux line breaks:

$log = "Started`r`nCopied 12 files`nFinished"
$lines = $log -split "`r?`n"

"Lines: $($lines.Count)"
"Second line: $($lines[1])"

Output:

Lines: 3
Second line: Copied 12 files

To remove the line breaks instead, see how to remove a newline from a string.

Watch out for mixed line endings in files

Set-Content ends each line with CRLF on Windows. If your string also has `n inside it, the file ends up with both kinds:

function Show-Breaks([string]$Text) { $Text.Replace("`r", '\r').Replace("`n", '\n') }

$text = "Line one`nLine two"

$text | Set-Content -Path C:\psfaqs\notes.txt
'Before: ' + (Show-Breaks ([System.IO.File]::ReadAllText('C:\psfaqs\notes.txt')))

$text -replace "`r?`n", "`r`n" | Set-Content -Path C:\psfaqs\notes.txt
'After:  ' + (Show-Breaks ([System.IO.File]::ReadAllText('C:\psfaqs\notes.txt')))

Output:

Before: Line one\nLine two\r\n
After:  Line one\r\nLine two\r\n

Most editors handle this, but diff tools and older programs may not. Replacing "`r?`n" with "`r`n" before writing gives you a clean Windows file.

Frequently Asked Questions

How do I add a new line in a PowerShell string?

Put `n inside a double-quoted string, like "Line one`nLine two". For Windows text files, use `r`n.

Why is `n printed as text instead of a new line?

The string is in single quotes. Escape sequences like `n only work in double-quoted strings.

What’s the difference between `n and `r`n in PowerShell?

`n is a line feed (LF). `r`n is a carriage return plus line feed (CRLF), the standard line ending for Windows files.

How do I replace spaces with new lines in PowerShell?

Use $text -replace '\s+', "`n". It also handles tabs and repeated spaces. For single spaces, $text.Replace(' ', "`n") works too.

How do I write multiple lines to a file in PowerShell?

Pass an array to Set-Content, like 'Line one', 'Line two' | Set-Content -Path notes.txt. Each item goes on its own line.

You might also find these string guides useful:

Leave a Comment