Delete an MSOL User via PowerShell

Scenario

In this article, we are going to show you how to delete a user using PowerShell in Microsoft 365. This technique can be useful if you want to account for the user in Active Directory but don’t want to keep their data or profile around.

Users in Microsoft 365 have an associated username and password that allows them to access the various services that are available on-premises or as part of Office365. If you want to remove a user from your organization, you don’t need to do it manually; you can use PowerShell to manage users.

Keep in mind that this operation will delete all the data and profiles associated with the user. If you need to retain any of that information, you should create a new profile for the user instead.

In summary,

  • You want to delete user using PowerShell
  • You want to purge delete user from the recycle bin

 

Steps

Cloud Users

  1. Connect to MSOL service via PowerShell

  2. Run cmdlets below for a cloud users.

    #soft delete, note replace DemoUser with the UPN of your target user
    Remove-MsolUser -UserPrincipalName '[email protected]' -RemoveFromRecycleBin
    
    #check 
    Get-Msoluser -ReturnDeletedUser |fl
    
    #check  2nd stage recycle bin and display UPN
    Get-Msoluser -ReturnDeletedUser |select UserPrincipalName
    
    #get 2nd stage recycle bin then delete all object in the list
    Get-Msoluser -ReturnDeletedUser | Remove-MsolUser -RemoveFromRecycleBin -Force
    

DirSynced Users

  1. Access your Active Directory server where you have configured your Azure AD Connect Sync

  2. Run cmdlets below for a DirSynced users.

    #note replace DemoUser with the UPN of your target user
    Get-msoluser -UserPrincipalName '[email protected]' | Remove-MsolUser -Force | Remove-MsolUser -RemoveFromRecycleBin
    

    m365-delete-msol-user-powershell-img-1

  3. Perform Full or Delta sync.

  4. Wait for a few minutes, the MSOL object in the cloud should be deleted as well thereafter

 

References