Home
Remove deleted user from Office 365 recycle bin
Office 365 (Deleted) Monday, 27 April 2020 by paul

Sometimes it is neccesary to remove Office 365 accounts and then recreate them. For example if a Azure AD guest user account has been created with the wrong account type.

The following powershell script will generate a list of users in the Deleted Users section (https://portal.office.com/adminportal/home#/deletedusers) and allow one to be easily selected to be removed - as there is no option for this in the admin center.

# Remove deleted user from Office365 Recycle Bin

Param( 
    [Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true)] 
    [string] $username
) 

$test=Get-MsolDomain -ErrorAction SilentlyContinue
if($?)
{
	Write-Host "Already connected to MSOL" -foreground green
}
else
{
	Write-Host "Connecting to MSOL" -foreground yellow
    Connect-MsolService
}

if ($username -eq "") {
	$user = Get-MsolUser -ReturnDeletedUsers | select UserPrincipalName,DisplayName,UserType,SoftDeletionTimestamp | sort SoftDeletionTimestamp -Descending | Out-Gridview -PassThru -Title "Select User to remove from Recycle Bin (Esc to abort)"
} Else {
	$user = Get-MsolUser -UserPrincipalName $username
}
if ($user -ne $null) {
   Write-Host "Removing user"$user.UserPrincipalName"from recycle bin" -foreground green
   $user | Remove-MsolUser -RemoveFromRecycleBin -Force
} Else {
   Write-Host "No user selected. Aborting." -foreground red
}

 


Add Comment
No Comments.