Wednesday, 10 February 2016

Permanently Delete a User in Office 365 using powershell

When you delete users in Office 365 admin Portal, these deleted users are moved to the Recycle Bin where they will be retained for another 30 days before that user is permanently deleted. In the Office 365 admin portal, it is not possible to permanently remove users or purge deleted user accounts. However, you can do it through remote powershell.

Note: Before proceed, Install and Configure Azure AD PowerShell

Use the following command to connect the Azure Active Directory:
Use the following command to retrieve a list of all deleted users. This command will list all the users that are currently stored in the Recycle Bin folder, and have not deleted permanently because 30 days have not passed since they were moved to this folder.
Get-MsolUser –ReturnDeletedUsers
Once you get the deleted users list, you can decide certain users to delete permanently from this list and you can use the cmdlet Remove-MsolUser to remove a specific deleted user with the parameter -RemoveFromRecycleBin.
Remove-MsolUser -UserPrincipalName -RemoveFromRecycleBin -Force
You can remove all the deleted users by fetching all deleted users using Get-MsolUser cmdlet and pipe the output to Remove-MsolUser.
Get-MsolUser -ReturnDeletedUsers | Remove-MsolUser -RemoveFromRecycleBin -Force
You can also filter the deleted users with specific criteria and remove those users permanently.
Get-MsolUser -ReturnDeletedUsers | Where-Object { $_.Department -like '*DevTeam*'} | 
Remove-MsolUser -RemoveFromRecycleBin -Force


No comments:

Post a Comment