Let’s look at how to open, read, and parse emails in a connected Outlook mailbox from within a PowerShell script. Outlook MAPI allows you to directly access the mailbox, list mailbox items, and read the emails (including the sender’s address, subject, body, etc.).
In order for PowerShell to have access to the contents of the mailbox, you must have Outlook running on the computer. Check whether the outlook.exe process is running and run it in the background by using the command:
$OutlookProc = ( Get-Process | where { $_.Name -eq "OUTLOOK" } )
if ( $OutlookProc -eq $null ) { Start-Process outlook.exe -WindowStyle Hidden; Start-Sleep -Seconds 5 }
Now you need to load the .NET class and create an Outlook instance:
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
The contents of the mailbox are accessible via the MAPI protocol namespace:
$namespace = $Outlook.GetNameSpace("MAPI")
There can be multiple folders in a mailbox. List mailbox folders:
$NameSpace.Folders.Item(1).Folders | FT FolderPath
You can display a list of folders in a tree view and count the number of email items in each folder:
Function Listfolders { param($Folders, $Indent) ForEach ($Folder in $Folders | sort-object name) { write-host $Indent$($Folder.Name)" ("$($Folder.Items.Count)")" Listfolders $Folder.Folders $Indent" " } } ListFolders $namespace.Folders ""
To find out the default name of the folder for incoming emails, run the following command (by default, this is the Inbox folder, but this name may vary depending on your language/regional settings):
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
List emails in the Inbox folder in the following format: sender address, recipient, subject, size, and date received.
$inbox.Items | ft SenderEmailAddress, To, Subject, Size, ReceivedTime
Simple PowerShell Where-Object filter allows you to search for specific emails. For example, list emails received today from a specific sender:
$currentDate = Get-Date
$inbox.Items | Where-Object { $_.ReceivedTime -like "*$currentDate*" -and $_.SenderEmailAddress -eq "[email protected]"}
You can display the subject and body of the email. The email body can be displayed in plain text (Body
property) or in HTML format (HTMLBody
property). In this example, the body of the most recently received e-mail will be displayed:
$inbox.Items($inbox.Items.Count)|select SenderEmailAddress,subject,Body,HTMLBody|fl
If the email has an attachment, you can save the attachment file to a local drive:
$email= $inbox.Items($inbox.Items.Count)
if ($Email.Attachments.Count -gt 0) {
$Attachment = $Email.Attachments.Item(1)
$Attachment.SaveAsFile("C:\Downloads\$($Email.Attachments.Item(1).FileName)")
}
Delete the last received email from the mailbox:
$email= $inbox.Items($inbox.Items.Count)
$Email.Delete()
You can use PowerShell to access your Outlook mailbox in automation scripts that require you to perform specific actions when you receive an incoming email. Use the Task Scheduler task to run a PS1 script to check your mailbox.