Quantcast
Channel: o365info
Viewing all articles
Browse latest Browse all 368

Force delete Microsoft 365 mailbox with PowerShell

$
0
0

You get an error when you force delete a Microsoft 365 mailbox from the recycle bin with PowerShell Exchange Online. The problem is that most hard deleted mailboxes are not entirely correctly removed. This article will show you how to permanently force delete user, shared, room, and equipment mailbox with PowerShell.

Force hard delete Microsoft 365 mailbox error

Let’s look at how the force hard delete mailbox error appears with PowerShell.

Note: Before permanently deleting any mailbox, you must soft delete it to find it in the Azure AD recycle bin.

In our example, we have soft deleted all types of mailboxes, and we want to delete them permanently from the recycle bin.

1. Start Windows PowerShell as administrator and run the cmdlet Connect-ExchangeOnline to Connect to Exchange Online PowerShell.

Connect-ExchangeOnline

2. Sign in with your credentials.

3. Run the below command to get a list of all the soft deleted mailboxes with PowerShell.

Get-Mailbox -SoftDeletedMailbox

The PowerShell output result is shown below.

PS C:\> Get-Mailbox -SoftDeletedMailbox

Name                      Alias           Database            ProhibitSendQuota    ExternalDirectoryObjectId           
----                      -----           --------            -----------------    -------------------------           
Melissa Kelly             Melissa.Kelly   EURPR02DG235-db031  99 GB (106,300,44... 3c790bcf-48d2-44f7-b1f7-e06c18bbe097
Projector 11              Projector11     EURPR02DG257-db113  49.5 GB (53,150,2... 2c849a06-2d71-48ca-b35e-21c894b93072
EquipmentTest             EquipmentTest   EURPR02DG506-db110  49.5 GB (53,150,2... 3745e1c5-8764-4f75-914a-46f69ffeb20d
Info Box3                 InfoBox3        EURPR02DG493-db376  49.5 GB (53,150,2... bb7fd29a-ec5a-407f-aaf4-34ce4bbc97ea
Projector 21              Projector21     EURPR02DG202-db028  49.5 GB (53,150,2... 628cd9d3-3994-4469-8794-f089396bae40
c474ceb9-a536-4526-83d... Amanda.Hansen   EURPR02DG180-db069  49.5 GB (53,150,2... c474ceb9-a536-4526-83db-8c72ed1604a8
Julia Wood                Julia           EURPR02DG528-db077  99 GB (106,300,44... 99df7b19-ab34-4bc2-8d69-d7de1382cbee
Info Box                  InfoBox         EURPR02DG504-db416  49.5 GB (53,150,2... bd8a36bd-cd76-4e69-8162-f700e268ee79

4. The PowerShell syntax below will show an error for all mailbox types (user, shared, room, and equipment), and it will not remove the hard deleted mailboxes from this list.

Get-Mailbox -Identity "user@domain.com" | Remove-Mailbox -PermanentlyDelete -Force -Confirm:$false

This is what it looks like in the output.

PS C:\> Get-Mailbox -Identity "Julia.Wood@m365info.com" | Remove-Mailbox -PermanentlyDelete -Force -Confirm:$false

Write-ErrorMessage : Ex6F9304|Microsoft.Exchange.Configuration.Tasks.ManagementObjectNotFoundException|The operation couldn't be performed because object 'Julia.Wood@m365info.com' couldn't 
be found on 'HE1PR02A11DC002.EURPR02A011.PROD.OUTLOOK.COM'.
At C:\Users\AppData\Local\Temp\tmpEXO_01jl4f0q.nce\tmpEXO_01jl4f0q.nce.psm1:1121 char:13
+             Write-ErrorMessage $ErrorObject
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=AM9PR02MB7203,RequestId=cc70c628-35c2-74ca-45ad-6df02e5db1ba,TimeStamp=Wed, 15 Mar 2023 22:59:05 GMT],Write-ErrorMessage

Why do we get the error when we want to delete the mailbox permanently with PowerShell?

Let’s take a look at the solution in the next step.

Solution permanently force delete Microsoft 365 mailbox with PowerShell

The solution to force delete any mailbox with PowerShell is to hard delete it first and then permanently remove it. Follow the steps below to see how you can hard delete a mailbox with PowerShell without an error.

1. Connect to Azure AD with PowerShell

Start Windows PowerShell as administrator and run the cmdlet Connect-MsolService to Connect to Azure AD PowerShell.

Connect-MsolService

Fill in the administrator credentials and sign in.

Force delete mailbox error with PowerShell sign in

2. Display all soft deleted mailboxes

Before we can hard delete a mailbox, we need the information value of the parameters -UserPrincipalName and -ObjectId.

Therefore we will display a list of all soft deleted mailboxes from the Azure AD recycle bin.

Run the below PowerShell command to get all soft deleted mailboxes.

Get-MsolUser -All -ReturnDeletedUsers | Select Displayname,UserPrincipalName,ObjectId |ft

The results of the PowerShell output look like the example shown below.

PS C:\> Get-MsolUser -All -ReturnDeletedUsers | Select Displayname,UserPrincipalName,ObjectId |ft 

DisplayName   UserPrincipalName                                                    ObjectId                            
-----------   -----------------                                                    --------                            
Amanda Hansen Amanda.Hansen@m365info.com                                           c474ceb9-a536-4526-83db-8c72ed1604a8
Info Box      ExRemoved-bbb7d65e6c8148dca7e8dd2a3216c0cc@ms365info.onmicrosoft.com bd8a36bd-cd76-4e69-8162-f700e268ee79
Info Box3     ExRemoved-64477f816fec42dba49f49bfc8977563@ms365info.onmicrosoft.com bb7fd29a-ec5a-407f-aaf4-34ce4bbc97ea
Projector 11  ExRemoved-c313d792e43243cb8f56c7b0b80aa0ee@ms365info.onmicrosoft.com 2c849a06-2d71-48ca-b35e-21c894b93072
Projector 21  ExRemoved-b632283276da4357aaae2c646898baa4@ms365info.onmicrosoft.com 628cd9d3-3994-4469-8794-f089396bae40
EquipmentTest ExRemoved-249e8a70ed5f4da98d1e97fb68149b9a@ms365info.onmicrosoft.com 3745e1c5-8764-4f75-914a-46f69ffeb20d
Julia Wood    ExRemoved-f62eb6868e674d33a94a50f078d523eb@ms365info.onmicrosoft.com 99df7b19-ab34-4bc2-8d69-d7de1382cbee
Melissa Kelly ExRemoved-69f44afbaf1f4dfb973bc7b005af6635@ms365info.onmicrosoft.com 3c790bcf-48d2-44f7-b1f7-e06c18bbe097

3. Hard delete mailbox with PowerShell

This above list will only display the soft deleted mailboxes, as seen in the Azure Active Directory recycle bin. In our example, we want to hard delete the user mailbox of Julia Wood, so we need to copy and use the UserPrincipalName or ObjectId.

There are two ways to hard delete a mailbox with PowerShell.

1. Hard delete a mailbox with the below PowerShell syntax.

Remove-MsolUser -UserPrincipalName "userprincipalname" -RemoveFromRecycleBin -Force

Copy the UPN value and run the below PowerShell example to hard delete a mailbox.

Remove-MsolUser -UserPrincipalName "ExRemoved-f62eb6868e674d33a94a50f078d523eb@ms365info.onmicrosoft.com" -RemoveFromRecycleBin -Force

2. Hard delete a mailbox with the below PowerShell syntax.

Remove-MsolUser -ObjectId "objectid" -RemoveFromRecycleBin -Force

Copy the ObjectId value and run the below PowerShell example to hard delete a mailbox.

Remove-MsolUser -ObjectId "99df7b19-ab34-4bc2-8d69-d7de1382cbee" -RemoveFromRecycleBin -Force

4. Check soft deleted mailbox removed from recycle bin

After running one of the above commands, we can check if the deleted user is not in the Azure AD recycle bin list.

Run the below PowerShell command, and the output shows no mailbox with a DisplayName Julia Wood.

PS C:\> Get-MsolUser -All -ReturnDeletedUsers | Select Displayname,UserPrincipalName,ObjectId |ft

DisplayName   UserPrincipalName                                                    ObjectId                            
-----------   -----------------                                                    --------                            
Amanda Hansen Amanda.Hansen@m365info.com                                           c474ceb9-a536-4526-83db-8c72ed1604a8
Info Box      ExRemoved-bbb7d65e6c8148dca7e8dd2a3216c0cc@ms365info.onmicrosoft.com bd8a36bd-cd76-4e69-8162-f700e268ee79
Info Box3     ExRemoved-64477f816fec42dba49f49bfc8977563@ms365info.onmicrosoft.com bb7fd29a-ec5a-407f-aaf4-34ce4bbc97ea
Projector 11  ExRemoved-c313d792e43243cb8f56c7b0b80aa0ee@ms365info.onmicrosoft.com 2c849a06-2d71-48ca-b35e-21c894b93072
Projector 21  ExRemoved-b632283276da4357aaae2c646898baa4@ms365info.onmicrosoft.com 628cd9d3-3994-4469-8794-f089396bae40
EquipmentTest ExRemoved-249e8a70ed5f4da98d1e97fb68149b9a@ms365info.onmicrosoft.com 3745e1c5-8764-4f75-914a-46f69ffeb20d
Melissa Kelly ExRemoved-69f44afbaf1f4dfb973bc7b005af6635@ms365info.onmicrosoft.com 3c790bcf-48d2-44f7-b1f7-e06c18bbe097

To double-check it, you can also run the below PowerShell command.

Get-Mailbox -SoftDeletedMailbox | ft

Note: Wait a few minutes before you run the PowerShell command Get-Mailbox -SoftDeletedMailbox to see the correct results.

The PowerShell output result looks different because it also displays the hard deleted mailboxes.

PS C:\> Get-Mailbox -SoftDeletedMailbox | ft

Name                      Alias           Database            ProhibitSendQuota    ExternalDirectoryObjectId
----                      -----           --------            -----------------    ------------------------- 
Melissa Kelly             Melissa.Kelly   EURPR02DG235-db031  99 GB (106,300,44... 3c790bcf-48d2-44f7-b1f7-e06c18bbe097
Projector 11              Projector11     EURPR02DG257-db113  49.5 GB (53,150,2... 2c849a06-2d71-48ca-b35e-21c894b93072
EquipmentTest             EquipmentTest   EURPR02DG506-db110  49.5 GB (53,150,2... 3745e1c5-8764-4f75-914a-46f69ffeb20d
Info Box3                 InfoBox3        EURPR02DG493-db376  49.5 GB (53,150,2... bb7fd29a-ec5a-407f-aaf4-34ce4bbc97ea
Projector 21              Projector21     EURPR02DG202-db028  49.5 GB (53,150,2... 628cd9d3-3994-4469-8794-f089396bae40
c474ceb9-a536-4526-83d... Amanda.Hansen   EURPR02DG180-db069  49.5 GB (53,150,2... c474ceb9-a536-4526-83db-8c72ed1604a8
Julia Wood                Julia           EURPR02DG528-db077  99 GB (106,300,44... 
Info Box                  InfoBox         EURPR02DG504-db416  49.5 GB (53,150,2... bd8a36bd-cd76-4e69-8162-f700e268ee79

Check if the ExternalDirectoryObjectId for the hard deleted mailbox is empty. In our example, Julia Wood has no ExternalDirectoryObjectId value, meaning the user mailbox is hard deleted.

Note: Suppose you already hard deleted a mailbox from the Azure AD recycle bin before, and you might still find it in the soft deleted mailbox list. The only difference is that the mailbox is without an ExternalDirectoryObjectId.

It means the mailbox is not entirely deleted permanently because you must complete one last step.

5. Permanently force delete Microsoft 365 mailbox with PowerShell

The last step is to delete the mailbox without an ExternalDirectoryObjectId from this soft deleted mailbox list.

Force hard delete any mailbox type with the below PowerShell syntax.

Get-Mailbox -Identity "user@domain.com" -SoftDeletedMailbox | Remove-Mailbox -PermanentlyDelete -Force -Confirm:$false

Run the PowerShell command example to delete a mailbox permanently.

Get-Mailbox -Identity "Julia.Wood@m365info.com" -SoftDeletedMailbox | Remove-Mailbox -PermanentlyDelete -Force -Confirm:$false

You successfully force deleted a mailbox with PowerShell without an error.

Verify Microsoft 365 mailbox deleted permanently

To verify that you removed the mailbox permanently, you must wait a minute before running the below cmdlet.

Run the below PowerShell command to display a list of all soft deleted mailboxes.

Get-Mailbox -SoftDeletedMailbox

Check if the hard deleted mailbox is on the list. For example, if you permanently deleted the user mailbox Julia.Wood@m365info.com, you should not find it in this list anymore.

The PowerShell output look like this.

PS C:\> Get-Mailbox -SoftDeletedMailbox

Name                      Alias           Database            ProhibitSendQuota    ExternalDirectoryObjectId
----                      -----           --------            -----------------    -------------------------
Melissa Kelly             Melissa.Kelly   EURPR02DG235-db031  99 GB (106,300,44... 3c790bcf-48d2-44f7-b1f7-e06c18bbe097
Projector 11              Projector11     EURPR02DG257-db113  49.5 GB (53,150,2... 2c849a06-2d71-48ca-b35e-21c894b93072
EquipmentTest             EquipmentTest   EURPR02DG506-db110  49.5 GB (53,150,2... 3745e1c5-8764-4f75-914a-46f69ffeb20d
Info Box3                 InfoBox3        EURPR02DG493-db376  49.5 GB (53,150,2... bb7fd29a-ec5a-407f-aaf4-34ce4bbc97ea
Projector 21              Projector21     EURPR02DG202-db028  49.5 GB (53,150,2... 628cd9d3-3994-4469-8794-f089396bae40
c474ceb9-a536-4526-83d... Amanda.Hansen   EURPR02DG180-db069  49.5 GB (53,150,2... c474ceb9-a536-4526-83db-8c72ed1604a8
Info Box                  InfoBox         EURPR02DG504-db416  49.5 GB (53,150,2... bd8a36bd-cd76-4e69-8162-f700e268ee79

Now you can permanently force delete every mailbox you already hard deleted from the Azure AD recycle bin.

Did this help you to solve your force delete mailbox error with PowerShell?

Read more: Manage Office 365 recycle bin with PowerShell »

Conclusion

You learned to force delete Microsoft 365 mailbox with PowerShell. The solution to this error is to connect to MsolService with PowerShell to hard delete the mailbox. After that, you need to force delete the mailbox permanently with PowerShell Exchange Online.

Did you enjoy this article? You may also like Manage Office 365 Shared Mailbox with PowerShell. Don’t forget to follow us and share this article.


Viewing all articles
Browse latest Browse all 368

Trending Articles