Let’s examine the process of opening, reading, and parsing emails from a connected Outlook mailbox using a PowerShell script. The direct access to the mailbox is facilitated by Outlook MAPI, which not only lists the mailbox items, but enables you to read the emails, including details such as the sender’s address, subject, and body among others.
Please note that for this scenario to work, Outlook must be installed on the computer, with a configured mailbox profile set up. The mailbox can be read from any mail server, including but not limited to Exchange, Outlook.com, Gmail, AOL, Yahoo.
To allow PowerShell access to the contents of the mailbox, Outlook should be running on the computer. Ensure that the outlook.exe process is in operation and running it in the background using the appropriate command.
$OutlookProc = ( Get-Process | where { $_.Name -eq "OUTLOOK" } )
if ( $OutlookProc -eq $null ) { Start-Process outlook.exe -WindowStyle Hidden; Start-Sleep -Seconds 5 }
Subsequent to this, your job is to load the .NET class and generate an Outlook instance:
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
You can reach the contents of the mailbox through 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 showcase a tree view of folders and tally the number of emails each contains:
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 ""
In order to discern the default name assigned to the email receiving folder, execute this command (normally, this is the Inbox folder, however, the name might differ depending on your language/region settings):
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
rel=”nofollow” target=”_blank”>List emails in the Inbox folder in the following format: sender address, recipient, subject, size, and date received.
rel="nofollow" target="_blank">$inbox.Items | ft SenderEmailAddress, To, Subject, Size, ReceivedTime
rel=”nofollow” target=”_blank”>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
Advertisements can be inserted for monetization.
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.