PowerShell: Create an email
In this article we are doing to perform two demonstrations for creating draft emails in Outlook from PowerShell.
- Option 1: Create an email draft however you need to open Outlook to see it.
- Option 2: Create an email draft but have the email open for you instead of opening Outlook.
Option 1
Here is the first set of commands (Option 1):
$Outlook = New-Object -comObject Outlook.Application
$TlabEmail = $Outlook.CreateItem(0)
$TlabEmail.To = "User1@thexchangelab.com"
$TlabEmail.Subject = "Draft email from PowerShell"
$TlabEmail.Body = "PowerShell makes your life easy"
$TlabEmail.save()
https://everything-powershell.com/wp-content/uploads/2020/12/image-18.png
Option 2
Here is the second set of commands (Option 2) with 2x extra lines at the end:
$Outlook = New-Object -comObject Outlook.Application
$TlabEmail = $Outlook.CreateItem(0)
$TlabEmail.To = "User1@thexchangelab.com"
$TlabEmail.Subject = "Draft email from PowerShell"
$TlabEmail.Body = "PowerShell makes your life easy"
$TlabEmail.save()
$inspector = $TlabEmail.GetInspector
$inspector.Display()
https://everything-powershell.com/wp-content/uploads/2020/12/image-19.png
The last two lines for the inspector is what opens up the Outlook shown below:
https://everything-powershell.com/wp-content/uploads/2020/12/image-20.png
The lines of code that has the To, Subject and Body info in is what you see above.