Export a mailbox to a target mailbox folder with EWS Managed API, in Exchange Online

In case you have used Search-Mailbox to export your mailbox data into a target mailbox folder and you encountered issues, this solution may turn to be a good alternative.

 

Prerequisistes
-The script requires EWS Managed API 2.2, which can be downloaded here: https://www.microsoft.com/en-gb/download/details.aspx?id=42951

-You will connect to the service with an Exchange Online mailbox that needs to be granted full access rights on the source and target mailboxes.

 

 

 DISCLAIMER: This application is a sample application. The sample is provided "as is" without warranty of any kind. Microsoft further disclaims all implied warranties including without limitation any implied warranties of merchantability or of fitness for a particular purpose.The entire risk arising out of the use or performance of the samples remains with you. in no event shall Microsoft or its suppliers be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss arising out of the use of or inability to use the samples, even if Microsoft has been advised of the possibility of such damages). Because some states do not allow the exclusion or limitation of liability for consequential or incidentaldamages, the above limitation may not apply to you.



$SourceMailbox = Read-Host -Prompt "Source mailbox"
$TargetMailbox = Read-Host -Prompt "Target mailbox"

Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2013_SP1

#Provide the credentials 
$credential = Get-Credential -Message "Provide the credentials of the Exchange Online mailbox that has full access permissions on the source and on the target mailboxes"
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $credential.UserName, $credential.GetNetworkCredential().Password

#Exchange Online URL
$service.Url= new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")


#Create the Target folder
$TargetFolderName = Read-Host -Prompt "Type the name of the Target folder"

$TargetMsgFolderRootId= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$TargetMailbox)

$TargetFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)

$TargetFolder.DisplayName = $TargetFolderName

$TargetFolder.Save($TargetMsgFolderRootId)


#Source mailbox Traversal
$SourceFolderId= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$SourceMailbox)

$FolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)

$FolderView.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Shallow

$FindFolderResults = $service.FindFolders($SourceFolderId,$FolderView)


foreach($Folder in $FindFolderResults.Folders)

{
Write-Host "Copying the $($Folder.DisplayName) folder to the target mailbox folder" -ForegroundColor White

$SourceFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$Folder.Id)

$Folder.Copy($TargetFolder.Id)

Write-Host "The source folder and its subfolders were successfully copied under the specified target folder" -ForegroundColor White
}