Migrate emails between accounts ( Exchange-office365 online) using dates

Hi ;

Today I will leave a handy script that will move emails between accounts on office365 ;
This could be of use on the scenario that you want to archive emails on another account ; or just simply migrate

There are some requirements though prior performing this action;

1.”Mailbox Import Export” Membership

2.”Mailbox Search” Membership

In order to have a unique shoot on this; We will go into the Exchange admin centre / Permissions / Admin roles and we will create a new Role as displayed:

2018-11-01 11_11_08-Role Group - Waterfox

Once this is accomplished ( Remember to exit your PS session or either close the PowerShell windows to renew the security tokens ) if not, we will pass to the next stage;

Open PowerShell ISE ( or simply PowerShell )

$Usercredential: Your Office365 Portal Admin credentials
$Sourceusername : Emails from this account will be copied based in the date variables to the destination account
$TargetUsername: Email target
$Foldername: ( folder on target where will be copied the content )
$Fromdate: Email range date “from”
$Todate : Email range date “TO”

So in this case we will be copying all of the 2017 emails from test to “movehere” ; afterwards this emails will be deleted from “test”

Please do pay attention at the last line as per after the copy we will need to empty the office365 dumpster if our changes are wanted to be reflected as soon as possible;

( Search-mailbox -identity “test@yourcompany.com” -SearchDumpsterOnly -DeleteContent)


$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$SourceUserName="test@yourcompany.com";
$TargetUserName="movehere@yourcompany.com";
$TargetFolderName="2017";
$Fromdate="01/01/2017";
$Todate="31/12/2017";
$date="Received:"+$Fromdate+".."+$Todate
Search-Mailbox $SourceUserName -SearchQuery "$date" -TargetMailbox "$TargetUserName" -TargetFolder "$TargetFolderName" -LogLevel Full -DeleteContent -Confirm:$false

Search-mailbox -identity "test@yourcompany.com" -SearchDumpsterOnly -DeleteContent
# After finishing remember to perform: Remove-PSSession $Session

 

Enjoy !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s