[Powershell Script] Import eml file to user mailbox (specific folder) using EWS managed API

Import email we exported as ".eml" using the script we discussed in my last post below into user mailbox using PowerShell script.

Exporting Emails from Office 365 and Exchange Mailbox Purges folder using EWS managed API

you can add the credentials to PowerShell sessions in "$cred" variable this script will get your admin credentials automatically, you can provide them at the time of running using -UserName and -Password parameters.

Usage Example :Password from the Console sessions
$cred = Get-Credential
Import-Eml.ps1 -$mailbox "Test@domain.com" -importFromFolder "C:\mails" -SaveInFolder "ImportedEmails"

Usage Example:  Specific Userid and password for current Run only.
Import-Eml.ps1 -$mailbox "Test@domain.com" -importFromFolder "C:\mails" -SaveInFolder "ImportedEmails" -UserName "adminid@domain.com" -Password "AdminPass"

This script is going to use the EWS managed API, please met the below requirements.

  • Admin account should have application impersonation rights, you can follow this msdn post to setup the permissions.
  • Install EWS managed API on system where you plan to run the script from, if API is not installed, download the same from here

param (

$uri=[system.URI] "https://outlook.office365.com/ews/exchange.asmx"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath

## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$service.url = $uri
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `

#Get folder to import
$MailboxRootid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)
$allFolders=$findFolderResults | ? {$_.FolderClass -eq "IPF.Note" } | select ID,Displayname
$DI = $allFolders | ? {$_.DisplayName -eq $SaveinFolder}

#Crete Email Object
$emailsinfolder=Get-ChildItem $ImportFromFolder | ? {$_.name -like "*.Eml" }

write-host "Total Item Fount to be imported:" $emailsinfolder.count
foreach ($email in $emailsinfolder) {
$EmailtoImport= $importFromFolder + "\" + $email.Name
Write-host "Importing Email:" $EmailtoImport
$UploadEmail = new-object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
#Read File
[byte[]]$EmailinByte=get-content -encoding byte $EmailtoImport
#Set Mime Content in Message
$UploadEmail.MimeContent = new-object Microsoft.Exchange.WebServices.Data.MimeContent("us-ascii", $Emailinbyte);
$PR_Flags = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition `
(3591, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);


  1. Thanks for the script! Note that the comment get-content will take a huge amount of RAM and CPU when reading mails over 1 meg, and kill your server at over 10 meg, because by default it only read one line at time. I added the option " -Readcount 0 " to solve this issue and speed up the importation.


Post a Comment