Friday, January 15, 2021

How to copy old user’s mailbox to another user’s mailbox on Office 365 and Exchange Server using PowerShell

If you are an Exchange administrator then you will come across the issue with leavers mailboxes needing to be transfered to another users mailbox. If you are looking after on-site exchange, then the process is slightly shorter as you don’t need to connect to the remote powershell. You simply log in to one of you exchange servers and run PowerShell as Administrator. Then you run following command and check all completed correctly:

Search-Mailbox -Identity “Old User” -TargetMailbox “Current User” -TargetFolder “Old-Mailbox” -LogLevel Full

To do the same on Office 365 you need to connect to remote PowerShell.

Make sure your account is enabled for remote PowerShell. You will need to be a member of Office 365 Global Admins or you will need to ask one of the Global Admin users to enable remote powershell by running following command on the Remote PowerShell:

Set-User user@contoso.com -RemotePowerShellEnabled $true

Check TCP port 80 is open between your PC and Office 365

Run PowerShell on your computer as Administrator

First of all you will need to set your execution policy to Remote Signed

Set-ExecutionPolicy RemoteSigned

Then save youre credentials into a variable for later use, you will be prompted to insert them

$usercredential = Get-Credential

jnow we will save our session to a variable

$session = New-PSSession -configurationname Microsoft.Exchange -connectionUri https://outlook.office365.com/powershell/ -credential $usercredential -authentication Basic -allowredirection

And then we will import it 

Import-PSSession $session

Now we can execute any commands we require, so in our case we want to transfer messages from one users mailbox to another, which is identical to the on-site exchange

Search-Mailbox -Identity “Old User” -TargetMailbox “Current User” -TargetFolder “Old-Mailbox” -LogLevel Full