The Best Way To Automate and Email in PowerShell

Automate and Email in PowerShell

PowerShell is great for automating many tasks. You might get the results of those tasks quickly or they could take awhile. Wouldn’t it be nice to run the script and walk away knowing that when it has finished it will notify you or send the results of the task by email (even with attachments).
The following is the best way to Automate and Email in PowerShell. You might want to read a newer article about this. It is more secure.

Automate and Email in PowerShell Using Send-MailMessage

The task could be anything. The point is that you can take the results and email them using the Send-MailMessage command. It is not recommend for use anymore as it is considered obsolete, however I am sure there are a lot of scripts out there that still use it. With Microsoft disabling the use of basic authentication for email it would make this command more difficult to use but it is not impossible.

If you are familiar with Microsoft Graph, it is possible to tweak your script to use modern authentication. My example will show you how to send out through an authorized network that makes use of a mail connector in O365 which helps legacy devices and applications that cannot send mail with Modern Authentication. I will circle back shortly in another article and show this example again using MS Graph to help send using modern auth. In the meantime if you can use another SMTP server (like Gmail) to send your mail in this script, that would be great.

Automate and Email in PowerShell Example

In this example I am taking screencaps at intervals I specify and then emailing them to myself when it is done:

#counter for Loop

$cnt = 1

#number of screenshots you want to take

$tot = 2

#number of seconds you want to wait between screenshots

$wait = 2

#Number of Attachments Left to Screen Cap

$left = 0

#Attachment List

$att = “”

#filepath array

$filepath = @()

While ($cnt-ne $tot+1)


#Capture Screenshot

$File = “C:\Temp\Screenshot”+$cnt+”.bmp”
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

#Gather Screen resolution information

$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = $Screen.Width
$Height = $Screen.Height
$Left = $Screen.Left
$Top = $Screen.Top

#Create bitmap using the top-left and bottom-right bounds

$bitmap = New-Object System.Drawing.Bitmap $Width, $Height

#Create Graphics object

$graphic = [System.Drawing.Graphics]::FromImage($bitmap)

#Capture screen

$graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)

#Save to file

Write-Output “Screenshot saved to:”
Write-Output $File

$left = $tot – $cnt

$att = $File

$filepath += $att

Write-Host Will wait $wait seconds before next screenshot….
Write-Host There are $left to go…
Start-Sleep -seconds $wait



Write-Host $filepath

Send-MailMessage -From [email protected] -To [email protected] -Subject ‘Screenshots’ -Body ‘See Attached’ -Attachments $filepath -SmtpServer -Port 587

Write-Host All Done

Explanation Of The Script

First, the script sets all the counters at the users preference (the counter for the loop, how many screenshots you want to take and how many seconds between screenshots)

All that is left is to provide credentials for the SMTP Server , capture the screenshots and then send the email with the prescribed attachments. Automation at it’s best.

Happy IT’ing


Quick IT TIps!

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Avatar photo

I am an IT professional with over twenty years experience in the field. I have supported thousands of users over the years. The organizations I have worked for range in size from one person to hundreds of people. I have performed support from Help Desk, Network / Cloud Administration, Network Support, Application Support, Implementation and Security.

Leave a Comment

Pin It on Pinterest