Copy/Move Emails between Office 365 mailboxes using PowerShell

 Office 365, Exchange Online, Perumal
Dec 19 2015

In an enterprise, often there is a requirement for admins to copy the emails from one user mailbox to another, depending on cases like when new member is added to a project team or when a new replacement takes care of a previous employee’s job etc. So in these cases, admins need to copy emails from the source user mailbox/mailboxes to the target user’s mailbox folder. In this blog, we share the steps for copying or moving emails between Office 365 user mailboxes using PowerShell.

In this blog we use “Search-Mailbox” cmdlet to copy or move emails. Before starting the process, let’s have a glance on “Search-Mailbox” cmdlet. The Search-Mailbox cmdlet is used to search a mailbox and copy the results to a specified target mailbox, remove emails from the source mailbox after copying to the target mailbox.

PowerShell script to move/copy emails between Office 365 mailboxes

For executing the following PowerShell scripts, the user must be a member of “Discovery Management” group.
For example, if you need to use “DeleteContent” switch in “Search-Mailbox” cmdlet for removing the emails from source mailbox after copying to the target mailbox, then assign the “Mailbox Import Export” role to “Discovery Management” group.

Common Input Parameters:
$SourceUserName - Office 365 Source User Mailbox
$TargetUserName - Office 365 Target User Mailbox
$TargetFolderName - Target Folder of Office 365 Target User Mailbox
Copy emails between Office 365 mailboxes based on Subject

Example:
In this example, I use “Search-Mailbox” cmdlet to search Taylor’s mailbox for emails that contain the phrase “Project Tasks” in the subject and copy those emails to “Project” folder of Tom’s mailbox.

Input Parameters Required
$SubjectName - Subject name to be searched in Office 365 Source User Mailbox
For common parameters refer – “Common Input Parameters” section.

$SourceUserName="Taylor"; 
$TargetUserName="Tom";
$TargetFolderName="Project";
$SubjectName ="Project Tasks";
Search-Mailbox $SourceUserName -SearchQuery "$SubjectName" -TargetMailbox "$TargetUserName" -TargetFolder
"$TargetFolderName" -LogLevel Full -SearchDumpster:$false

NOTE: By default, the Recoverable Items folder is always searched. To exclude Recoverable Items folder from the search, set the “SearchDumpster” switch to $false.

Copy emails between Office 365 mailboxes based on Date Range

Example:
In this example, I use “Search-Mailbox” cmdlet to search Ben’s mailbox for emails between the date ranges from 1-October-2015 to 30-November-2015 and copy those emails to “Project” folder of Mike’s mailbox.

Input Parameters required:
$Fromdate, $Todate - Date Range to be searched in Office 365 Source User Mailbox
For common parameters refer – “Common Input Parameters” section.

$SourceUserName="Ben";
$TargetUserName="Mike";
$TargetFolderName="Project";
$Fromdate ="10/01/2015";
$Todate = "11/30/2015";
$date="Received:"+$Fromdate+".."+$Todate
Search-Mailbox $SourceUserName -SearchQuery "$date" -TargetMailbox "$TargetUserName" -TargetFolder "$TargetFolderName" -LogLevel Full

Copy all emails between Office 365 mailboxes

Example:
In this example, I use “Search-Mailbox” cmdlet to search all emails in Susan’s mailbox and copy those emails to “Project” folder of Tina’s mailbox.

Input Parameters required:
For common parameters refer –“Common Input Parameters” section.

$SourceUserName="Susan";
$TargetUserName="Tina";
$TargetFolderName="Project";
Search-Mailbox $SourceUserName -TargetMailbox "$TargetUserName" -TargetFolder "$TargetFolderName=" -LogLevel Full

Move emails between Office 365 mailboxes based on Subject

Example:
In this example, I use “Search-Mailbox” cmdlet to search Watson’s mailbox for emails that contain the phrase “Project” in the subject and copy those emails to “Project Tasks” folder of Sherlock’s mailbox. Then using “DeleteContent” switch emails copied to the target mailbox are removed from the source mailbox.

Input Parameters required:
$SubjectName - Subject name to be searched in Office 365 Source User Mailbox
For common parameters refer –“Common Input Parameters” section.

$SourceUserName="Watson";
$TargetUserName="Sherlock";
$TargetFolderName="Project";
$SubjectName ="Project Tasks";
Search-Mailbox $SourceUserName -SearchQuery "$SubjectName" -TargetMailbox "$TargetUserName" -TargetFolder
"$TargetFolderName" -DeleteContent -Confirm:$false