Skip to content

How to send an email using PowerShell and Gmail

Brock Bingham candid headshot
Brock Bingham|March 28, 2025
Hero image illustration of PowerShell logo with rings.
Hero image illustration of PowerShell logo with rings.

There’s not much that PowerShell can’t do, and that includes sending emails. However, with the deprecation of Send-MailMessage, the process has changed. Here’s a new method to send emails with PowerShell and a Gmail account using the PoshMailKit module.

The deprecation of Send-MailMessage

The cybersecurity landscape is constantly changing, which means security protocols need to change with it. Unfortunately, Microsoft hasn’t updated Send-MailMessage to support modern security protocols. Instead, they decided to deprecate the cmdlet and recommend users rely on external projects like MailKit moving forward. Gee, thanks Microsoft. (Wait, how do I make that sound even more sarcastic?)

PoshMailKit: The Send-MailMessage PowerShell alternative

Thankfully, the PowerShell community isn’t one to sit around and wait for a fix. My favorite Send-MailMessage alternative I’ve found so far is PoshMailKit by poshcodebear on GitHub.

PoshMailKit is essentially a drop-in replacement for the Send-MailMessage cmdlet. It’s based on MailKit but developed to seamlessly replicate the feature set and parameter usage of Send-MailMessage. Essentially, once you have the module installed, you can simply replace the command Send-MailMessage with Send-MKMailMessage to achieve the same result.

ConnectIcon CTA

Easily run PowerShell scripts on remote devices

Need to run your awesome PowerShell scripts on remote devices? PDQ Connect can easily execute PowerShell scripts on any managed device with an active internet connection. 

1. Install PoshMailKit

If you’ve ever installed a PowerShell module, then you’ve got all the skills required to install the PoshMailKit module. If not, no worries. Installing PowerShell modules is easier than falling down a flight of stairs. (Hurts less, too.) Here’s the one liner command that’ll get the job done:

Install-Module -Name PoshMailKit

Simply run that command in a PowerShell terminal with elevated rights, then enter “Y” when you are asked to confirm the installation.

Installing the PoshMailKit with PowerShell.

And you can double-check to see if the module is already installed by using this command:

Get-Module -ListAvailable -Name PoshMailKit
Verifying the PoshMailKit module is installed.

2. Create a Google app password

If you have 2-Step Verification turned on in your Google account, you’ll need to create an app password in order to send an email with PowerShell using your Gmail account.

You can create a Google app password on the app passwords management page. Simply create a new entry and take a note of the password that is generated. From what I can tell, this is the only time you’ll be able to see this password, so don’t lose it.

Creating a Google app password that will be used to send emails with PowerShell.

3. Send an email with PowerShell and PoshMailKit

Alright, now that we have the PoshMailKit module installed and our handy dandy Google app password, we’re ready to email some people with PowerShell. Here’s the script we’ll use:

#Sending email with PowerShell #Import PoshMailKit module Import-Module PoshMailKit #Set email parameters $From = "sender_address@gmail.com" $To = "target_address@email.com" $Subject = "Test PowerShell email" $Body = "This is a test email sent using PowerShell and PoshMailKit." #Define SMTP (gmail) server info $SMTPServer = "smtp.gmail.com" $SMTPPort = 587 $Credential = Get-Credential #Command to send email Send-MKMailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -Credential $Credential -UseSsl

The script is very similar to how you would use the deprecated Send-MailMessage command. The biggest differences being you need to first import the PoshMailKit module and then use the Send-MKMailMessage command. The Get-Credential command will request your account credentials, which is where you’ll provide the Google app password you created in the previous step.

As you can see, I’m running this script in VS Code, but feel free to use any PowerShell script editor, such as ISE. This script should work in Windows PowerShell and PowerShell 7.x.


Now the only question is, what will you do with your newfound PowerShell skill? I recommend spamming the spammers. After all, revenge is a dish best served with PowerShell. Or, if you’re looking for something a little less risky, maybe just stick it to Microsoft by removing the new Outlook. That’ll teach them to deprecate their own cmdlets.

Brock Bingham candid headshot
Brock Bingham

Born in the '80s and raised by his NES, Brock quickly fell in love with everything tech. With over 15 years of IT experience, Brock now enjoys the life of luxury as a renowned tech blogger and receiver of many Dundie Awards. In his free time, Brock enjoys adventuring with his wife, kids, and dogs, while dreaming of retirement.

Related articles