How to Send an Email Using Windows PowerShell

Ben Stegner 11-12-2019

Email is so basic that most people don’t even think about it. Open up your mail client, type a message and recipient, and hit send. Not too tough, right?


But have you ever considered alternate ways to send emails? Whether you just want to geek out and do something techy or want to send an email without getting distracted by your inbox, there’s a useful tool for it hiding right on your Windows desktop. It’s called PowerShell.

How to Send an Email Using Windows PowerShell PowerShell Send Email

How to Send an Email Using PowerShell

  1. Open a PowerShell window by searching for PowerShell in the Start Menu.
  2. Use the below Gmail template to set up your email. The first lines that start with dollar signs set up variables for sending the message, while the Send-MailMessage line is the actual command:
    $From = ""
    $To = ""
    $Cc = ""
    $Attachment = "C:\users\Username\Documents\SomeTextFile.txt"
    $Subject = "Here's the Email Subject"
    $Body = "This is what I want to say"
    $SMTPServer = ""
    $SMTPPort = "587"
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential) -Attachments $Attachment –DeliveryNotificationOption OnSuccess
  3. If using Yahoo mail, the server is and the port is 465. For Outlook, the server is with port 587.
  4. You can remove the Attachment and CC lines if you don’t need them. Make sure to remove them from the Send-MailMessage line too.
  5. Including –UseSsl makes sure the email sense securely. You can also include -DeliveryNotificationOption OnSuccess if you want to receive confirmation that it sent successfully.
  6. Once you issue the command, it will prompt you for your username and password thanks to(Get-Credential). Enter that, and your email is on its way!

For another geeky way to do this, check out how you can send an email with Google Sheets How to Send Email in a Google Sheet With Google Scripts Learn how to use Google Scripts to automate Google productivity tools like Sheets and Docs and save yourself hours of time. Read More . And on the subject of PowerShell, did you know that PowerShell can double up as a hash checker for checking the integrity of a file?

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Anil
    April 17, 2020 at 8:20 am

    Great job, this code works for me.
    Thanks Ben.

  2. Kayla
    July 18, 2019 at 4:59 pm

    what would be needed if this needs to run at a certain time each day?

  3. Nitin
    April 5, 2019 at 1:02 pm

    Hi Ben,
    What if we do not want to supply the credentials but we want to add the credentials to scripts only. How to achieve this?

  4. Tarvinder Singh
    March 23, 2019 at 1:46 pm

    Very nice and simple. Thanks.
    Although gmail used 2 step authentication now and this would probably not run for any website running 2 step authentication.

  5. Baljeet Singh
    February 3, 2018 at 2:57 pm

    very very good and easy, expained very nicely. Thanks