How to Permanently Remove Deleted Users in Office 365 Using PowerShell

How to Permanently Remove Deleted Users in Office 365 Using PowerShell - Microsoft 365

When you delete a user from the Office 365 control panel, they are temporarily moved to a recycle bin for 30 days, allowing for easy recovery if the deletion was not intended.

However, if you find yourself needing to permanently remove a deleted user in Office 365, PowerShell comes to the rescue.

In this guide, we will walk you through the steps to permanently remove deleted users using PowerShell and the Azure Active Directory for PowerShell module.

Connecting to Azure Active Directory

To get started, we need to establish a connection to your Azure Active Directory. Don’t worry, it’s a straightforward process. Open PowerShell and run the command Connect-MsolService. You will be prompted to enter your admin credentials in a dialog box.

PS C:Scripts> Connect-MsolService

Caution: Take a Moment to Think

Viewing Deleted Users

Before we dive into the user removal process, it’s crucial to pause and reflect. Removing users permanently is a serious action that cannot be undone easily. Please proceed only if you are absolutely sure about removing the users.

To see a list of the deleted users in Office 365, execute the following PowerShell command:

PS C:Scripts> Get-MsolUser -ReturnDeletedUsers

This will provide you with a list of deleted users, giving you an overview of who can be permanently removed.

Removing a Specific Deleted User

To permanently remove a specific deleted user, you can use the Remove-MsolUser command with the -RemoveFromRecycleBin switch. Replace UserPrincipalName with the actual user’s UPN (User Principal Name) you wish to remove:

PS C:Scripts> Remove-MsolUser -UserPrincipalName Lynn@office365bootcamp.com -RemoveFromRecycleBin

Confirmation Prompt Before the deletion is finalized, PowerShell will ask for your confirmation. You will see a prompt like this:

 Confirm                                                                                                                                  Continue with this operation?                                                                                                        [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

Removing All Deleted Users

If you want to remove all the deleted users at once, you can utilize the power of PowerShell’s piping functionality. Run the following command, which combines Get-MsolUser and Remove-MsolUser with the -Force switch to bypass individual confirmation prompts:

PS C:Scripts> Get-MsolUser -ReturnDeletedUsers | Remove-MsolUser -RemoveFromRecycleBin -Force

Conclusion

Congratulations! You have learned how to permanently remove deleted users in Office 365 using PowerShell. Remember, PowerShell gives you the flexibility and control you need to manage user data effectively.

However, it’s crucial to exercise caution and double-check before executing any deletion commands to avoid accidental removal of critical users or data. By following these steps, you can confidently manage user accounts in Office 365 while maintaining proper data security.

Previous Article

How to Push a Local Docker Image to Azure Container Registry

Next Article

PowerShell Script: Batch Processing of Office 365 Users with Progress Tracking

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Powered by Amail.