Create and view Outlook emails with PowerShell

YouTube

Basic

The following PowerShell script is designed to create and display a fixed form mail.
In actual use, change the values of To, CC, Subject, and Body.

CreateMail.ps1

# Please change parameters
$To = "<To>"
$CC = "<CC>"
$Subject = "<Subject>"
$Body = "<Body>"
 
function CreateMailByOutlook {
    $Outlook = New-Object -ComObject Outlook.Application
    $Mail = $Outlook.CreateItem(0)
    $Mail.To = $To
    $Mail.CC = $CC
    $Mail.Subject = $Subject
    $Mail.Body = $Body
    $inspector = $Mail.GetInspector
    $inspector.Display() # Use Display() instead of Send() to send the message after a visual confirmation in Outlook
}
 
function Main {
    CreateMailByOutlook
}
 
Main

ExecuteCreateMail.bat

It is convenient to put the following batch file together so that you can double-click the batch file to run it.

pushd %~dp0
powershell -NoProfile -ExecutionPolicy RemoteSigned .\CreateMail.ps1
popd

Sample : If you want to take annual leave on the next day

CreateMailForHoliday.ps1

Create and display an email with the details of taking annual leave on the next day.
However, if it is a Friday or some other day, the content will be changed to the next Monday instead of the next day.

# Create holiday data
$now = Get-Date
switch ($now.DayOfWeek.value__) {
    # Friday
    5 {$date = $now.AddDays(3)}
    # Saturday
    6 {$date = $now.AddDays(2)}
    Default {$date = $now.AddDays(1)}
}
$dateString = "{0:yyyy/MM/dd}" -f $date
$dayOfWeekString = $date.ToString("ddd")
 
 
# Create E-mail
$outlook = New-Object -ComObject Outlook.Application
$mail = $outlook.CreateItem(0)
 
$mail.To = "Example mailing list <example@example.com>"
 
$mail.CC = ""
 
$mail.Subject = "[Absence contact] Annual leave $dateString ($dayOfWeekString)"
 
$mail.Body ="For personal reasons, I will be taking annual leave.
 
Thank you.
"
 
$inspector = $mail.GetInspector
$inspector.Display()