
Ever wished you could fire off an email without opening Outlook, Gmail, or even touching your browser? Well, PowerShell’s got your back! With just a few lines of code, you can send emails straight from your terminal like a true tech wizard.
Today, we’ll walk through a simple PowerShell script that lets you send emails via the amazing Powershell terminal. Whether you’re automating notifications, sending alerts, or just having fun with scripts, this method is a game-changer.
The Magic PowerShell Script
Here’s the script that makes email automation a breeze:
$EmailMessage = @{
To = "user@mail.com"
From = "me@mail.com"
Subject = "Test Email"
Body = "Test email sent using Powershell"
SmtpServer = "anySMTPserver.mail.com"
Port = "25"
}
$username = "your_username"
$password = "your_password"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($username, $securePassword)
Send-MailMessage @EmailMessage
Let’s Break it Down
- 📨 To & From: Defines the sender and recipient email addresses.
- ✉ Subject & Body: The subject and body content of the email.
- 📡 SMTP Server & Port: Specifies the SMTP relay server (replace it with your own).
- 🔒 Authentication: Converts the password into a secure string and stores credentials.
- 🚀 Send-MailMessage: The command that makes the magic happen!
But, but.. Now Everyone sees my Password
Don’t worry. If you are looking for a method to hide your password we can rewrite the script in a few easy steps. Let’s add the “$credentials = Get-Credential” command. This will hide the password.
$credentials = Get-Credential
$EmailMessage = @{
To = "user@mail.com"
From = "me@mail.com"
Subject = "Test Email"
Body = "Test email sent using Powershell"
SmtpServer = "anySMTPserver.mail.com"
Port = "25"
Credential = $credentials
}
Send-MailMessage @EmailMessage
Why is this better?
✅ No plaintext passwords – You’re prompted securely for your username and password.
✅ More secure automation – Store credentials in a variable for reuse within your session.
✅ Less risk – No risk of exposing credentials in scripts or logs.
What about Secure Email like STARTTLS?
Ah, yes!
You want to send the email securely, or the SMTP server demands it.
Got it.. We can add that too by changing one line, and adding one line.
$credentials = Get-Credential
$EmailMessage = @{
To = "user@mail.com"
From = "me@mail.com"
Subject = "Test Email"
Body = "Test email sent using Powershell"
SmtpServer = "anySMTPserver.mail.com"
Port = "587"
UseSsl = $true
Credential = $credentials
}
Send-MailMessage @EmailMessage
Why Use STARTTLS?
✅ Encrypts email in transit – Prevents eavesdropping.
✅ More secure than plain SMTP (port 25) – Ideal for Office 365 and most email providers.
✅ Ensures compliance – Many services now require STARTTLS for authentication.
And there you have it! With this simple PowerShell script, you can send emails like a pro, automating notifications, system alerts, or even prank emails (responsibly, of course!).
Now, go forth and email away!