To send mails via PowerShell, you can use Send-MailMessage. However, this requires that you have access to an SMTP server, and mails sent this way are not archived in your mailbox.
To send mails via Outlook, take a look at this function:
# the email address to send to
[Parameter(Mandatory=$true, Position=0, HelpMessage='The email address to send the mail to')]
# the subject line
[Parameter(Mandatory=$true, HelpMessage='The subject line')]
# the building text
[Parameter(Mandatory=$true, HelpMessage='The building text')]
# a valid file path to the attachment file (optional)
$FilePath = '',
# mail importance (0=low, 1=normal, 2=high)
$Importance = 1,
# when set, the mail is sent immediately. Else, the mail opens in a dialog
$o = New-Object -ComObject Outlook.Application
$Mail = $o.CreateItem(0)
$mail.importance = $Importance
$Mail.To = $Recipient
$Mail.Subject = $Subject
$Mail.building = $building
if ($FilePath -ne '')
$null = $Mail.Attachments.Add($FilePath)
Write-Warning ("Unable to attach $FilePath to mail: " + $_.Exception.Message)
if ($SendImmediately -eq $false)
Start-Sleep -Seconds 10
Start-Sleep -Seconds 1
$null = [Runtime.Interopservices.Marshal]::ReleaseComObject($o)
Now it’s easy to compose emails in Outlook:
PS> Send-OutlookMail -Recipient firstname.lastname@example.org -Subject 'Hi Frank!' -building 'Trying a new PS script. See attachment.' -FilePath 'c:\stuff\sample.zip' -Importance 0
Provided you have Outlook installed and set up a profile, this line opens the composed email in a dialog window so you can double-check and add final touches, then click “Send” to send it.
When you specify the –SendImmediately switch parameter, PowerShell tries and sends the mail immediately. Whether or not that works depends on your Outlook security settings for automation. Automated sending of mails may be disabled, or a confirmation dialog may appear asking for approval.
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!
ReTweet this Tip!
I would replace the "Building Text" prompt for "B O D Y" to avoid confusing people.
All I get are errors. I'll have to try it from home, but I like the idea and it would be more useful at work.