Delete deleted user accounts in Office 365 using PowerShell

To delete deleted user accounts in Office 365 using PowerShell, you can use the following script:

# Connect to Office 365
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential

# Get a list of deleted user accounts
$DeletedUsers = Get-MsolUser -ReturnDeletedUsers

# Iterate through the list of deleted user accounts
foreach ($DeletedUser in $DeletedUsers)
{
    # Permanently delete the user account
    Remove-MsolUser -ObjectId $DeletedUser.ObjectId -RemoveFromRecycleBin
}

# Disconnect from Office 365
Disconnect-MsolService

This script will prompt you for your Office 365 login credentials, then it will retrieve a list of all deleted user accounts in your tenant. It will then iterate through this list and permanently delete each user account. Finally, it will disconnect from Office 365.

Please note that this operation is irreversible, so make sure you want to permanently delete these user accounts before running the script.

Leave a Comment