PowerShell: Restore Soft Deleted Exchange On-line Mailbox To New Microsoft 365 User

Recently, I encountered a situation in which a Microsoft 365 account had been deleted. Then, a new account with the same name was created and synchronized from the local domain. The user’s local profile and other software were linked to the new account and everything was working. The only issue was the mailbox missing all of its content.

Believe it or not, this isn’t an uncommon situation in my line of work. I looked online for a script and found lots of examples. Much to my surprise, I struggled to get any of them to work. Rather than try to troubleshoot and fix one , I opted to write my own. While writing it, I believe I discovered the issue. Exchange Online does not except the ExchangeGUID as a name/value pair. It has to be plain text.

Below is what I came up with. You will need to have the Exchange Online PowerShell Module installed on your computer. Open an Admin PowerShell Session and run the one-liner below to install it. You may also be prompted to update PowerShell itself if your computer has fallen behind in updates.

Install-Module ExchangeOnlineManagement

Save and run the script below. First, it will prompt for the tenant’s modern authentication global admin credentials. Next, it will prompt you for the account your are working with’s identity. You should be able to use the first and last name, UPN, or alias address. The script will use that data to locate the Exchange GUID for the source and target mailboxes. Finally, it will create and run a mailbox restore request and show the status.

# Restore Soft Deleted Mailbox to a new Matching Account in Exchange Online
# Assumes the Alias for both accounts is the same

#Sign in to Microsoft 365 Exchange.
#Requires Microsoft Exchang Online PowerShell 
$GlobalAdminUPN = Read-Host "Enter Global Admin UPN account"
Connect-ExchangeOnline -UserPrincipalName $GlobalAdminUPN

#Aquire the ExchangeGUID attribute for the source and target mailboxes
$UserAlisas = Read-Host "Enter mailbox identifier"
$SourceMailboxGuid = [string](Get-Mailbox -SoftDeletedMailbox $UserAlisas).ExchangeGuid
$TargetMailboxGuid = [string](Get-Mailbox -Identity $UserAlisas).ExchangeGuid

#Restore Softdeleted mailbox over the new Account's mailbox

New-MailboxRestoreRequest -SourceMailbox $SourceMailboxGuid -TargetMailbox $TargetMailboxGuid -AllowLegacyDNMismatch

#Check the restore job status
Get-MailboxRestoreRequest | Get-MailboxRestoreRequestStatistics

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 )

Facebook photo

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

Connecting to %s