title: Powershell using string and file content in email message body without breaking formatting
body: <p>I have a Powershell script that has a part which needs to email some information. I have a requirement to include 2 pieces of information inside the message body, but I am having issues with formatting. The first part will be a line of text which I would like to simply store inside the powershell script as a variable, and the second part will be information from a text file. Below is a simplified version of what I want to do:</p>
  
  <pre><code>$INFO1="The following fruits are ready to eat:
  
  
  "
  $INFO2=get-content "info2.txt"
  $BODY=$INFO1 + $INFO2
  $BODY=$BODY | out-string
  send-mailmessage -To myname@mydomain.com -Subject "Fruit Status" -From 
  fruit@mydomain.com -Body $BODY -Smtpserver smtp.mydomain.org -UseSsl
  </code></pre>
  
  <p>The file info2.txt contains the following info. 3 columns with a list of fruits with tab separators:</p>
  
  <pre><code>Col1        Col2        Col3
  Apple       Orange      Pear
  Banana      Grape       Strawbery
  Kiwi        Peach       Apricot
  </code></pre>
  
  <p>The above code works but I get an email which looks like this:</p>
  
  <pre><code>The following fruits are ready to eat:
  
  
  Col1        Col2        Col3 Apple      Orange      Pear Banana     Grape       
  Strawbery Kiwi      Peach       Apricot
  </code></pre>
  
  <p>It keeps the line spaces in the $INFO1 stored variable string, but then places the content of the info2.txt file all in one continuous line, with no line breaks or tabbed formatting. </p>
  
  <p>I tested this with the below changes whereby the line of text from the $INFO1 variable, is instead obtained by doing a get-content on the same piece of text from inside a file named "info1.txt". When I then append both pieces of information into the message body, the formatting is maintained and the email looks ok.</p>
  
  <pre><code>$INFO1=get-content "info1.txt" 
  $INFO2=get-content "info2.txt"
  $BODY=$INFO1 + $INFO2
  $BODY=$BODY | out-string
  send-mailmessage -To myname@mydomain.com -Subject "Fruit Status" -From 
  fruit@mydomain.com -Body $BODY -Smtpserver smtp.mydomain.org -UseSsl
  
  
  
  The following fruits are ready to eat:
  
  
  Col1        Col2        Col3 
  Apple       Orange      Pear 
  Banana      Grape       Strawbery 
  Kiwi        Peach       Apricot*
  </code></pre>
  
  <p>I also notice that if I do the below using the first method, grabbing the file content first and THEN the string from variable, the formatting is also kept fine:</p>
  
  <pre><code>$BODY=$INFO2 + $INFO1
  </code></pre>
  
  <p>It is only when I start with the string from variable first, that it mucks up the formatting in the email for the file content part.</p>
  
  <p>I would prefer to keep the script more compact and minimize writing out to files on disk and used stored variables. Is there a way I can use method 1 appending a string or multiple strings, followed by file content where formatting is kept? I do not wish to use HTML for this as it will overly complicate matters with the text file content I am working with so the body is just -body  rather than -bodyashtml unless this is the only way to sort this.</p>