[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7354
  • Last Modified:

Get line breaks in powershell output to email

I have a text file that is being read by powershell that has varying number of lines in it. I want powershell to read the lines and output to an email that lists each line on its own line.

The script below emails the lines all as one line and Ihave not been able to get it to show the line breaks.

Any help would be greatly appreciated.

$Line = Get-Content E:\MonitorFolder\PingResults.txt | Out-String  
 
  Send-MailMessage -From 'Lost_Network_Connection@company.com' -To 'me@systrends.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Lost Network Connection' `
  -BodyAsHtml `
    -Body @"
      $(($Line) + "'n")  
"@
0
jimmylew52
Asked:
jimmylew52
  • 3
  • 2
1 Solution
 
footechCommented:
Try the following.
$Line = Get-Content E:\MonitorFolder\PingResults.txt 
 
  Send-MailMessage -From 'Lost_Network_Connection@company.com' -To 'me@systrends.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Lost Network Connection' `
  -BodyAsHtml `
  -Body ($Line -join "`n")

Open in new window

0
 
jimmylew52Author Commented:
Two lines in PingResults.txt

server is down
Station is down

Displays in email as :

server is downstation is down

instead of two lines.

 -Body ($Line -join "`n")  --  does the same thing.
0
 
footechCommented:
Oh wait, just noticed that you're outputting HTML.  One moment.
0
 
footechCommented:
Make sure you're not piping to Out-String in line 1.
Change line 7 to
  -Body ($Line -join "<br>")

Open in new window

0
 
jimmylew52Author Commented:
That worked!!!   Thank You!!!
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now