SOLVED

Removing shared mailboxes

%3CLINGO-SUB%20id%3D%22lingo-sub-2726261%22%20slang%3D%22en-US%22%3ERemoving%20shared%20mailboxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2726261%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20to%20remove%20several%20shared%20mailboxes%20from%20our%20tenant%20and%20I%20am%20going%20to%20do%20this%20via%20a%20PowerShell%20script.%20As%20you%20may%20know%2C%20every%20time%20you%20try%20to%20remove%20a%20mailbox%20using%20the%20%22remove-mailbox%22%20cmdlet%2C%20you%20will%20get%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3EThis%20mailbox%20cannot%20be%20permanently%20deleted%20since%20there%20is%20a%20user%26nbsp%3B%3C%2FFONT%3E%3CFONT%20color%3D%22%23FF0000%22%3Eassociated%20with%20this%20mailbox%20in%20Azure%20Active%20Directory.%20You%20will%20first%20need%20to%20delete%20the%20user%20in%20Azure%20Active%20Directory.%20Please%26nbsp%3B%3C%2FFONT%3E%3CFONT%20color%3D%22%23FF0000%22%3Erefer%20to%20documentation%20for%20more%20details.%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3ESo%20you%20have%20first%20to%20delete%20the%20user%20from%20Azure%2C%20then%20the%20mailbox.%20This%20is%20the%20code%20I%20usually%20run%3A%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24MbxToRemove%20%3D%20Get-Content%20-Path%20%22C%3A%5CTemp%5CMbxToRemove.txt%22%0Aforeach%20(%24mbx%20in%20%24MbxToRemove)%7B%0A%20%20%20%20Remove-MsolUser%20-UserPrincipalName%20%24mbx%20-Force%0A%20%20%20%20Start-Sleep%2015%0A%20%20%20%20Remove-MsolUser%20-UserPrincipalName%20%24mbx%20-RemoveFromRecycleBin%20-Force%0A%20%20%20%20Start-Sleep%2060%0A%20%20%20%20Remove-Mailbox%20-Identity%20%24mbx%20-PermanentlyDelete%20-Confirm%3A%24false%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20pause%20the%20script%20for%20one%20minute%20before%20running%20the%20Remove-Mailbox%20cmdlet%20otherwise%20the%20system%20will%20still%20see%20the%20%22deleted%20user%22.%20The%20txt%20file%20is%20a%20simple%20list%20with%20all%20the%20UserPrincipalName%20of%20the%20shared%20mailboxes.%3C%2FP%3E%3CP%3EI%20was%20wondering%20if%20there%20is%20a%20way%20to%20speed%20up%20things%20a%20bit%20(%3Dbetter%20code).%20When%20you%20have%20to%20process%20100%2F150%20mailboxes%2C%20this%20takes%20a%20certain%20amount%20of%20time.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks%20in%20advance%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFrancesco%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2726261%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExchange%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2727274%22%20slang%3D%22en-US%22%3ERe%3A%20Removing%20shared%20mailboxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2727274%22%20slang%3D%22en-US%22%3EThat's%20because%20you%20are%20using%20the%20-PermanentlyDelete%20switch.%20As%20the%20documentation%20says%2C%20this%20switch%20is%20used%20for%20removing%20soft-deleted%20mailboxes%3A%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20switch%20works%20only%20on%20mailboxes%20that%20have%20already%20been%20deleted%2C%20but%20are%20still%20recoverable%20(known%20as%20soft-deleted%20mailboxes).%20%3CBR%20%2F%3E%3CBR%20%2F%3ESimply%20do%20it%20like%20this%3A%3CBR%20%2F%3E%3CBR%20%2F%3ERemove-Mailbox%20shared%20-Force%3C%2FLINGO-BODY%3E
Contributor

Hello everyone,

 

I have to remove several shared mailboxes from our tenant and I am going to do this via a PowerShell script. As you may know, every time you try to remove a mailbox using the "remove-mailbox" cmdlet, you will get the following error:

 

This mailbox cannot be permanently deleted since there is a user associated with this mailbox in Azure Active Directory. You will first need to delete the user in Azure Active Directory. Please refer to documentation for more details.

 

So you have first to delete the user from Azure, then the mailbox. This is the code I usually run:

 

$MbxToRemove = Get-Content -Path "C:\Temp\MbxToRemove.txt"
foreach ($mbx in $MbxToRemove){
    Remove-MsolUser -UserPrincipalName $mbx -Force
    Start-Sleep 15
    Remove-MsolUser -UserPrincipalName $mbx -RemoveFromRecycleBin -Force
    Start-Sleep 60
    Remove-Mailbox -Identity $mbx -PermanentlyDelete -Confirm:$false
}

 

I need to pause the script for one minute before running the Remove-Mailbox cmdlet otherwise the system will still see the "deleted user". The txt file is a simple list with all the UserPrincipalName of the shared mailboxes.

I was wondering if there is a way to speed up things a bit (=better code). When you have to process 100/150 mailboxes, this takes a certain amount of time.

 

Many thanks in advance,

 

Francesco

  

3 Replies
best response confirmed by fstorer (Contributor)
Solution
That's because you are using the -PermanentlyDelete switch. As the documentation says, this switch is used for removing soft-deleted mailboxes:

This switch works only on mailboxes that have already been deleted, but are still recoverable (known as soft-deleted mailboxes).

Simply do it like this:

Remove-Mailbox shared -Force

Thank you @Vasil Michev!

What about the shared mailbox users in Azure? Do I always need to delete them first? Is there a quick way to delete them permanently in one step? 

The Remove-Mailbox cmdlet takes care of the user deletion as well.