Export Email Messages from OWA in Office 365 Exchange Online
Introduction
This script can export email messages from Outlook Web App to a specific folder. These email messages will be saved in .eml format. You need to find the email messages by using search folder.
Scenarios
Microsoft Outlook allows you to export email messages very easily. However, this feature is not available in the Outlook Web App (OWA). Therefore, in order to export email messages from OWA, you must find a workaround.
Prerequisites
This script requires Exchange Web Service Managed API 2.0. Please download and install the package from
Microsoft Download Center. This script cannot work correctly without this package.
Script
This script contains the following advanced functions:
- Connect-OSCEXOWebService
- New-OSCEXOSearchFolder
- Get-OSCEXOSearchFolder
- Export-OSCEXOEmailMessage
You can use this script in the following way.
- Open Windows PowerShell.
- Run Import-Module cmdlet to import this
module file.
Import-Module filepath\scriptname.psm1
Here are some code snippets for your references.
PowerShell
Edit|Remove
powershell
#Get the content and subject of email messages
$emailContent = $emailMsg.MimeContent.Content
$fileName = $emailMsg.Subject
foreach ($invalidChar in $invalidChars) {
$fileName = $fileName.Replace($invalidChar,"")
}
#If file already exists then add ticks to the end of the file name
if (-not $AllowOverwrite) {
$existingItemsCount = (Get-ChildItem -Path $Path -Filter "$fileName*" | Measure-Object).Count
if ($existingItemsCount -gt 0) {
$fileName = "$fileName - ($existingItemsCount)"
}
}
#Get the content and subject of email messages$emailContent = $emailMsg.MimeContent.Content
$fileName = $emailMsg.Subject
foreach ($invalidCharin$invalidChars) {
$fileName = $fileName.Replace($invalidChar,"")
}
#If file already exists then add ticks to the end of the file nameif (-not $AllowOverwrite) {
$existingItemsCount = (Get-ChildItem-Path $Path-Filter"$fileName*"|Measure-Object).Count
if ($existingItemsCount-gt 0) {
$fileName = "$fileName - ($existingItemsCount)"
}
}
Examples
Example 1: How to display help about
Export-OSCEXOEmailMessage
To display help about this function, run this command.
Get-Help Export-OSCEXOEmailMessage -Full

Example 2: How to initiate a connection to Office 365 Exchange Online.
To initiate a connection to Office 365 Exchange Online, please run this command. You must run this example before
any other step.
Connect-OSCEXOWebService -Credential (Get-Credential
admin@domain01.onmicrosoft.com)
Note The Connect-OSCEXOWebService function creates a new variable called exService. This variable is in the global scope of the current Windows PowerShell session. This variable is used by other functions in the script.
Example 3: How to create a search folder that contains the email messages with specific subject.
To create a search folder that contains the email messages with specific subject, please run this command.
New-OSCEXOSearchFolder -DisplayName "Subject contains 'Sales budget'" -Subject "Sales budget" -StartDate "10/01/2012 12:00:00 AM" -EndDate "12/31/2012 12:00:00 PM"
Example 4: How to export email messages in a search folder.
To export email messages in a search folder and keep this search folder after exporting, please run this command.
Get-OSCEXOSearchFolder -DisplayName "Subject contains 'Sales budget'" | Export-OSCEXOEmailMessage -Path C:\Scripts\055\Emails -KeepSearchFolder
Example 5: How to export email messages in a temporary search folder.
To export email messages in a temporary search folder, please run this command.
New-OSCEXOSearchFolder -Subject "Test email" | Export-OSCEXOEmailMessage -Path C:\Scripts\055\Emails
Additional Resources