power shell to read a txt file into msg body and send email

Posted on 2010-01-08
Last Modified: 2012-05-08
Hi ,
I need a power script to read a txt file and put the content of the txt file into message boy then email to me.

Please help!

Question by:changjia
    LVL 1

    Expert Comment

    Here is a basic way to do this.  Usage is .
    \sendFile.ps1 -file test.txt.
    [string]$fileData = Get-Content $file
    	Write-Host "Nothing in File."
    $msg = new-object
    $SMTPClient = new-object
    $SMTPServer = ""
    $ = $SMTPServer
    $msg.From = ""
    $msg.Subject = "Contents of $file"
    $msg.Body = $fileData

    Open in new window


    Author Comment

    Hi evetsleep:

    Thanks for the code. Could you explain to me what the param on line one does?
    Also, what does [string]$ file do?

    LVL 1

    Accepted Solution

    param is used to provide a parameter for the script.

    param([string]$file) allows for sendFile.ps1 -file  where -file is $file in param.  [string] basically tells PowerShell that the value entered after -file will be a string.  So if you were to do this:

    .\sendfile.ps1 -file messageBody.txt

    the string messgeBody.txt is stored in $file.  We then use Get-Content to .. well get the content of that file and store it in $fileData.

    You 'could' hard code the file name if you wanted, and not use param, but I find it more useful to allow for parameters

    Author Comment

    Got it. Thanks alot!
    So, I see there is a 'if' statement to check if the file is empty, don't send.
    My file has a header, so the first line always going to be in the file. How to check the file start from the second line?

    LVL 1

    Expert Comment

    could you give an example of what the file would look like?

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now