Formatting text

Welcome Forums General PowerShell Q&A Formatting text

This topic contains 3 replies, has 3 voices, and was last updated by

2 years, 10 months ago.

  • Author
  • #44449

    Topics: 30
    Replies: 61
    Points: 58
    Rank: Member


    I feel I have tried every which way to set the body of an email with the contents of two files, but not getting the expected result.

    I retrieve the content of each file and set the content of this data to a file called body.txt:

    get-content "C:\scripts\output\ $(get-date -f "MM_dd_yyyy")_US.txt" | Add-Content c:\scripts\output\body.txt
    get-content "C:\scripts\output\ $(get-date -f "MM_dd_yyyy")_MEX.txt" | Add-content c:\scripts\output\body.txt

    The file body.txt, if I open it in notepad, clearly shows CRLF at the end of each line. I then set the variable $body to a string by doing the following:

    $body = (get-content -path c:\scripts\output\body.txt) | out-string

    When I then try to email using send-mailmessage -body $body .... the body of the email contains all of my lines in one long string. I know the out-string does this, but the send-mailmessage requires the info to be a string.

    I will bet I am doing WAY too much manipulation of the data 🙂 ...

    Would someone please give me a suggestion?

  • #44458

    Topics: 2
    Replies: 49
    Points: 261
    Team MemberHelping Hand
    Rank: Contributor

    This works fine for me as long as the "-BodyAsHtml" parameter is not included with Send-MailMessage. If HTML is required, then some extra formatting steps may be needed.

  • #44473

    Topics: 30
    Replies: 61
    Points: 58
    Rank: Member

    Sorry I was not clear – the lines all come out in one string but I'd like them to appear in the body of the message as the original lines with CRLF for easy reading.

  • #44489

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    The body parameter requires a string value. Get-Content, by default, returs an Array of lines. These two methods worked for me:

    Adding the -Raw switch:

    $body = Get-Content C:\Users\Rob\Desktop\test.txt -Raw

    or using FileSystemObject, vbScript ole skool style:

    $body = (New-Object -Com Scripting.FileSystemObject).OpenTextFile("C:\Users\Rob\Desktop\test.txt",1).ReadAll()

The topic ‘Formatting text’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort