GSuite Microsoft Powershell

Send emails / SMS with Gmail and PowerShell with a Scheduled Task

A guide to setup automatic email / SMS reminder with Gmail and PowerShell.

Background story (that you can safely ignore)

I have been using scheduled tasks to send myself/other people email/SMS reminder for a long time already.  For example, I once used this task to send a SMS reminder to remind someone to claim her weekly Unemployment Insurance Benefit. 

In the past, I only need to set the Gmail account to allow less secure app to access and the script will work wonders.  Google is now stricter for allowing less secured apps.  You can still configure this setting, but they will turn it off automatically after a while and will break your automation.

Now that I must use this script again, I need to first generate an App password for my device. 

The Guide

  1. If you are using personal google account, you can go ahead and skip to step 3.  For G Suite users, the administrator will need to allow users to enable 2 step Verification first. 

Go to your G Suite Admin console and then Security -> Settings

  1. To allow 2-Step Verification, check the box to allow users to turn on 2-step Verification.  It is up to you (or your organization) if you want to enforce this setting.  For company, it maybe wise to enforce it to enhance security. 
  1. Now back to the user account settings and go to Security there.  Enable 2-step Verification and then click on App Passwords to set one up.
  1. There are two drop down menu. (at least for when this guide was written)  I chose “Mail” and “Windows Computer” because I will be using a Windows system and run a scheduled task to connect and send email using PowerShell.

Once we get the Gmail/Gsuite settings out of the way, we would work on the PowerShell Script. 

#Defining Variables.  
$Date = Get-Date -f dd #this will get us the current date
$Month = Get-Date -f MMMM  #this will get us the current month 
$Year = Get-Date -f yyyy   #this will get us the current year
$Today = $Month + ‘ ‘ + $date + ‘, ‘ + $year

$SMTPServer = ""
$Port = 587
$EmailFrom = "kenny" #replace this with the email address that you have the App Password setup.
$Pwd = "password" #replace this with the actual App password that you have just setup.
$EmailTo = "kenny"  #replace this with the email address or the Phone number (will explain how to send a text in a moment.)  You can add multiple email here, each separate by a comma “,”
$Subject = "Reminder!”  #replace the subject you want to put in the Email subject line.
$Body = "Today is $today!  Remember you need to do this!"  #replace the body of the email here.
$SMTPclient = New-Object Net.Mail.Smtpclient($SMTPServer, $port)
$smtpclient.enableSsl = $true
$SmtpClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, $Pwd);
$SMTPClient.Send($From, $To, $Subject, $Body)

  • On the computer that you will be running this script, open Task Scheduler and create a task.  In the action tab, set Action to “Start a program” and under “Program/Script”, type in powershell.  Next to “Add arguments (optional)” put in

-ExecutionPolicy -RemoteSigned -file “c:\script\reminder.ps1” -recurse

You should replace the file path with the location of your script.  I will leave the rest of the config to you. 

If you run the task now, you should receive an email reminder defined from the script.

To send text messages instead of an email, you will need to know the carrier the phone number is subscribed under.  For example, if 123-456-7890 is under Verizon, you can put in the $EmailTo section

Below is a list I found with the email Gateways for the more popular phone carriers in the U.S.  I believe this list is correct.  I can confirm T-Mobile, Verizon, and AT&T are correct because I have used them before. 

  • AT&T: (SMS), (MMS)
  • T-Mobile: (SMS & MMS)
  • Verizon: (SMS), (MMS)
  • Sprint: (SMS), (MMS)
  • Xfinity Mobile: (SMS), (MMS)
  • Virgin Mobile: (SMS), (MMS)
  • Tracfone: (MMS)
  • Simple Mobile: (SMS)
  • Mint Mobile: (SMS)
  • Red Pocket: (SMS)
  • Metro PCS: (SMS & MMS)
  • Boost Mobile: (SMS), (MMS)
  • Cricket: (SMS), (MMS)
  • Republic Wireless: (SMS)
  • Google Fi (Project Fi): (SMS & MMS)
  • U.S. Cellular: (SMS), (MMS)
  • Ting:
  • Consumer Cellular:
  • C-Spire:
  • Page Plus:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s