The quick version:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
New-Mailbox -Name "John Contoso" -RemovedMailbox "John Contoso" -MicrosoftOnlineServicesID John@contoso.com -Password (ConvertTo-SecureString -String 'NewPassword' -AsPlainText -Force)
The long version:
From time to time mistakes happen and, at least with Microsoft technology, there are ways of recovering data that has been deleted. Now, usually with Office 365 it’s a simple case of going to the Exchange server, viewing the deleted mailbox’s and clicking the recover button. However, if you are unlucky this method won’t work and you’ll have to fall back on to using powershell. If you have used Powershell before then you can skip to step 4, if not then start from the top:
1) Check the Execution policy The first thing you need to do is to check what execution policy you are using. The execution policy you use determines what scripts you can use. to do this you need to use the get command, like so: Get-ExecutionPolicy This will then display the your current execution policy. If it is set to strict then you’ll need to change the policy to remotesigned as you won’t be able to run any scripts. by setting it to remotesigned you can run any downloaded script that has been signed by a trusted publisher. To change the execution policy to need to type in the following: Set-ExecutionPolicy RemoteSigned You will be asked to confirm that you want to change the execution policy, just press “Y” and it should be changed. Once you’ve done that then you can move on to the next step.
2) Get Credentials This step is so that you can access your office 365 account that you are the administrator of. To do this you need to type in the following code: $UserCredential = Get-Credential A pop up box will appear asking for your email address and password. Type in the the email address that you use to access the office 365 of which you are an administrator. Note: it is important that you make sure you type in the correct information. If you don’t type it in correctly then when you do the next step you will get back an error and will have do do it all over again
3) Set up your session and importing it the next thing you need to do is configure your session. Essentially what this step is doing is connecting to the exchange server. to do this you need to type in the following code: $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Once you’ve entered that powershell will attempt to go and check the connection to your exchange server. Once that is done you can move on to importing the session. To import your session you need to type in the following: Import-PSSession $Session Once that is done powershell will go about importing your session so that you are fully connected up to the exchange server. Once it has finished importing you can move on to the next step.
4) Recovering the mailbox This is the script that you’ve probably been waiting for. This script will create a new account, find the deleted account and then recover all the data to the new account. To do this you need to type in the following code: New-Mailbox -Name “John Contoso” -RemovedMailbox “John Contoso” -MicrosoftOnlineServicesID John@contoso.com -Password (ConvertTo-SecureString -String ‘Pa$’ -AsPlainText -Force) Just replace the “John Contoso” with the details of the user mailbox that you are trying to recover. Once all the correct details are filled in press enter and get a message saying that it is trying to recover the mailbox. Note: it can take up to 8 hours for the mailbox to be recovered so it’s best to just leave it recovering for the day. In this time you won’t be able to access Outlook with the email address but you will be able to see it in the users section of Office 365. Just log in as the users every so often and check that you can get access to Outlook.