Under Reconstruction

# Sending Email with Outlook in PowerShell

Posted on 20 February 2016

One thing I very often find myself doing is sending personalised emails to a large group of students (or colleagues). Now, I can always use a MailMerge or something similar, but I’ve found that a simple PowerShell script that hooks onto Microsoft Outlook gets the job done in less amount of time and with less hassle!

To send a simple email, you can use the following:

$Outlook = New-Object -ComObject Outlook.Application # Create the Outlook COM object$Mail = $Outlook.CreateItem(0) # Create a new plain email$Mail.Recipients.Add("[email protected]") # Add a recipient. You can use this command as many times as you like.
$Mail.Subject = "Subject" # Set the subject. Obviously, you can use variables and the like in here$Mail.Body = "Content" # Set the body of the email. Again, you can use variables!
$Mail.Attachments.Add("c:\path\to\file.txt") # If you want to add an attachment, do so with this, as many time as you like$Mail.Save() # Save the email in the Drafts of Outlook
$Mail.Send() # Send the email! It’s as simple and easy as that. However, one of the more powerful features of Outlook is sending meeting requests. You can, of course, do that through PowerShell as well: $Outlook = New-Object -ComObject Outlook.Application # Create the Outlook COM object
$Meeting =$Outlook.CreateItem(1) # Create a new Meeting Request object
$Meeting.Recipients.Add("[email protected]") # Add recipients$Meeting.MeetingStatus = [Microsoft.Office.Interop.Outlook.olMeetingStatus]::olMeeting # Set the status to a "Meeting"

# Set the starting time. You can use whatever way you like, but it should be a PowerShell [datetime] object
$Meeting.Start = [datetime]::ParseExact("2017/02/02 16:15","yyyy/MM/dd HH:mm",$null)
$Meeting.Duration = 30 # The duration of the meeting, in minutes$Meeting.Location = "Location" # The location of the meeting
$Meeting.ReminderMinutesBeforeStart = 15 # Sets the number of minutes before the meeting to remind$Meeting.ReminderSet = $True # Sets the reminder$Meeting.Save() # Save the request in the Drafts of Outlook
\$Meeting.Send() # Send the meeting request!

There we go. Add a CSV file and some loops and you’re good to spam… I mean… send important emails to your students!