Send an Email with PowerShell

Applies to PowerShell
Modules needed: None

This is a fun little script I set up mostly to see if I could do it. It actually turned out to come in handy when I’m configuring email addresses and new mailboxes in exchange, and want to send a quick test email without changing windows.

The script asks for a recipient, a subject, and a body. It then confirms you want to send the email.

Note: The body is stored in HTML. This means if you want a line break, you need to type <br>in your line, as opposed to hitting ‘Enter’. 

You will want to change the “From”, “SmtpServer”, and “$Sig” lines to meet your needs. For the $sig variable, an HTML file containing your signature is expected.


Clear-Host
Write-Host "Send an Email" -ForegroundColor Yellow
Write-Host
Write-Host
$sig = Get-Content 
$to = Read-Host "To"
$subject = Read-Host "Subject"
$message = Read-Host "Message"
$body = $message + $sig
Write-Host
Write-Host
Write-Host "Message completed. Would you like to send?"
Write-Host
Write-Host "1) Yes"
Write-Host "2) No"
Write-Host
$selection = Read-Host "Select"
switch ($selection) {
'1' { Send-MailMessage -To $to -From  -Subject $subject -SmtpServer  -Body $body -BodyAsHtml
Write-Host "Message Sent." -ForegroundColor Green }
'2' { exit }
}

 

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s