44

I have an array of names that I'm trying to join using a new line character. I have the following code

$body = $invalid_hosts -join "`r`n"
$body = "The following files in $Path were found to be invalid and renamed `n`n" + $body

Finally, I send the contents via email.

$From = "[email protected]"
$To = "[email protected]
$subject = "Invalid language files"

Send-MailMessage -SmtpServer "smtp.domain.com" -From $From -To $To -Subject $subject -Body $body

When I receive the message, the line The following files in <filepath> were found to be invalid and renamed has the expected double space, but the contents of $invalid_hosts are all on one line. I've also tried doing

$body = $invalid_hosts -join "`n"

and

$body = [string]::join("`n", $invalid_hosts)

Neither way is working. What do I need to do to make this work?

2
  • This is not a duplicate as this problem specifically occurs in body of email when using Send-MailMessage in plain text mode. Commented May 25, 2021 at 7:08
  • 2
    Please notice that since Outlook 2003 double \n are removed by default. learn.microsoft.com/en-us/outlook/troubleshoot/message-body/… Commented Aug 9, 2023 at 8:12

8 Answers 8

38

Pipe the array to the Out-String cmdlet to convert them from a collection of string objects to a single string:

PS> $body = $invalid_hosts -join "`r`n" | Out-String
Sign up to request clarification or add additional context in comments.

5 Comments

@Robbert, you must have made a mistake. Try hard-coding $body and see what happens: $body = "This`nIs`nA`nTest"
It did the same thing I described above. In the end, I decided the issue may have been the formatting done by Outlook and changed the output to an HTML message. Instead of using a newline, I joined the array using <br/>.
Isn't join "`r`n" redundant to | Out-String? Why use both in the same command?
I had the same problem as @Robbert - it only worked for me when sending email as HTML with "<br/>" as separator when joining string array.
NB Out-String can truncate long lines based on the characteristics of the host program, so you may experience long variables been wrapped to another line - see the Width parameter.
19

It is sufficient just pipe to Out-String (see https://stackoverflow.com/a/21322311/52277)

 $result = 'This', 'Is', 'a', 'cat'
 $strResult = $result  | Out-String
 Write-Host $strResult
This
Is
a
cat

Comments

10

I'm unsure about how to answer everything else, but for guaranteed newlines in Powershell, use: [Environment]::NewLine in place of your "`n"

1 Comment

This worked for me, using `r`n simply output `r`n
7

Had to solve this today; thought I'd share my answer since the question and other answers helped me find the solution. Instead of

$body = $invalid_hosts -join "`r`n"
$body = "The following files in $Path were found to be invalid and renamed `n`n" + $body

use

$MessageStr = "The following files in " + $Path + " were found to be invalid and renamed"
$BodyArray = $MessageStr + $Invalid_hosts
$Body = $BodyArray -join "`r`n"

Comments

2

I went about it differently and just replaced the newline

$result -replace("`r`n"," ")

1 Comment

This would do the opposite, replacing new line characters with a space.
1

I am certainly no expert in PowerShell, but I found a much easier way to do it. Simply pipe to Write-Host like this:

$array = 'This', 'Is', 'a', 'cat'
$array | Write-Host

Output:
This
Is
a
cat

This is a slightly different use case than the OP question. It does not join the array with newlines, but it does give newlines when writing the output.

Comments

1

As already stated above [Environment]::NewLine is the only reliable way to create a line break in Powershell. You can do a -join([Environment]::NewLine) with your array, this avoids the extra line at the end of the string that Out-String creates.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
1

Old now, but this still comes up for certain Google search results.

With that in mind, one potential issue not addressed by other answers is when the message body is treated as an HTML message. In an HTML context, regular line breaks are ignored and you would need <br> or <p> entities.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.