38

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 = "myaddress@domain.com"
$To = "myaddress@domain.com
$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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robbert
  • 6,481
  • 5
  • 35
  • 61
  • This is not a duplicate as this problem specifically occurs in body of email when using Send-MailMessage in plain text mode. – maeni70 May 25 '21 at 07:08
  • Please notice that since Outlook 2003 double `\n` are removed by default. https://learn.microsoft.com/en-us/outlook/troubleshoot/message-body/line-breaks-are-removed-in-posts-made-in-plain-text – RedX Aug 09 '23 at 08:12

6 Answers6

34

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
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • @Robbert, you must have made a mistake. Try hard-coding $body and see what happens: `$body = "This\`nIs\`nA\`nTest"` – dan-gph Nov 20 '13 at 07:03
  • 1
    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 `
    `.
    – Robbert Nov 20 '13 at 18:07
  • 6
    Isn't `join "\`r\`n"` redundant to `| Out-String`? Why use both in the same command? – M-Pixel Jun 08 '20 at 16:18
  • I had the same problem as @Robbert - it only worked for me when sending email as HTML with "
    " as separator when joining string array.
    – maeni70 May 25 '21 at 07:12
  • 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. – Minkus Jun 14 '23 at 11:14
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
Community
  • 1
  • 1
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
8

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

user3012708
  • 793
  • 1
  • 11
  • 33
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"
George S
  • 71
  • 1
  • 1
3

I went about it differently and just replaced the newline

$result -replace("`r`n"," ")
grimanvil
  • 31
  • 2
2

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.

Jess
  • 23,901
  • 21
  • 124
  • 145