Regardless of how you accidentally deleted a person’s mailbox in Exchange, you can probably restore it using PowerShell (PoSh). The main thing is to do this as soon as you realize that the mailbox has been deleted.
This is the moment when we realize that we did it by accident. If you’ve only deleted a few emails, we can show you how to recover deleted emails in Office 365 too.
Before we begin, please note that this is for recovering deleted mailboxes in Exchange Online for an Office 365 and Exchange 2010 environment. We also assume that you have administrator rights to use the methods described and that you have completed some basic PowerShell scripts.
What happens when I delete a mailbox?
The mailbox is moved to either the Azure Active Directory (AD) Recycle Bin in Exchange Online or the Disabled Mailbox Directory in Exchange 2010.
It can stay there for up to 30 days, depending on retention policies in place. This gives us a grace period to admit our mistake and correct it.
Restore a deleted mailbox from Exchange Online
Connect to Exchange Online with PowerShell
In your local session, open a Powershell console and use the following command to specify the login credentials for the variable.
– / $ userCredential
This simplifies further work with other scripts.
$ userCredential = get-Credential
A window opens where you can enter your username and password to manage Exchange Online.
Then set the execution policy level so that we can actually do something in our session. This allows us to run our unsigned commands. But you can also learn the best practices for signing PowerShell scripts
When prompted, press Y for Yes.
We will now create a $ session variable that will be used to open a connection between the local computer and Exchange Online.
$ session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com / powershell-liveid / -Credential $ userCredential -Authentication Basic -AllowRedirection
Powershell-liveid is your Office site ID. This is usually a variation of your company name.
Use the following command to open a PoSh session in Exchange Online:
import-PSSession $ Session -DisableNameChecking
Now we work with PowerShell right in Exchange Online, even if we are sitting at our computer.
Recovering a deleted mailbox using PowerShell
The next part is very simple. It took us much longer to establish a connection.
All we need to do is run the cmdlet shown below:
undo-SoftDeletedMailbox firstname.lastname@example.org -WindowsLiveID email@example.com -Password (ConvertTo-SecureString -String ‘newpassword’ -AsPlainText -Force) < / pre>
Change both instances of firstname.lastname@example.org to the appropriate mailbox name and Windows LiveID of the mailbox we want to restore. Please note that they may not match.
We also need to set a new password for the mailbox. Change the new password in the script to a password of your choice. You will need to hand this over to the user and ask him to change the password the next time he logs in.
Finally, use the following cmdlet to make sure it worked.
If it was restored, the cmdlet will return information about the restored mailbox. If it returns an error, try the commands again and make sure you are using the correct mailbox and Windows LiveID.
If this does not help, we will need to restore the mailbox from a system backup. There are many types of system backups, so this is beyond the scope of what we do today.
When we’re done, we must close the PoSh session. The number of PoSh sessions launched is limited. If left open, one of these sessions is used. If we don’t close it, we’ll have to wait for the session to time out before we can start a new one.
remove-PSSession $ Session
This is it. You might even want to write this into a PowerShell module for ease of use if this happens again.
Recovering a deleted mailbox in Exchange 2010
This does not work for Office 365 or hybrid environments. It only works for on-premises Exchange 2010.
On the mail server, open the Exchange Management Console (EMC).
Navigate to Recipient Configuration> Disconnected Mailbox. There we should see the user’s mailbox.
If we don’t, then the process of cleaning up the mailbox database hasn’t happened yet. It’s okay, we can forcibly.
- Open the Exchange Management Shell (EMS) as an administrator. Here we can do the Exchange-specific PowerShell job.
- We now enter the cmdlet:
Get-MailboxDatabase | Clean-MailboxDatabase
- Once this is done, let’s go back to EMC and right click on the disabled mailbox and then click Refresh.
- We should now see a mailbox there, along with another one that was recently deleted.
- Go back to EMS and enter the cmdlet:
Connect-Mailbox -Identity “username” -Database “Mailbox Database” -User “username” pre>
- Username is the name of the Windows user account (for example, Test User), and Mailbox Database is the name of the database listed next to its name in the Disconnected Mailbox window in EMC.
- Refresh the disabled mailbox and you will see that their mailbox no longer appears there. Go to Recipient Configuration> Mailbox and verify that the user’s mailbox is present.
Check it to make sure all settings are correct, such as your email address and alias. If everything looks good, the next time the user opens their Outlook, everything will be there as it was.
Mailbox has been restored
Here’s how to use PowerShell to recover mailboxes in Exchange Online and Exchange 2010. If you’re in a hybrid environment, it’s a little more complicated, but it’s possible.
Just knowing that these different cmdlets exist gives you a good experience with Exchange, regardless of version or configuration.