Delete and restore user accounts with Office 365 PowerShell

 

Topic Last Modified: 2015-10-29

Learn how to use Office 365 PowerShell to delete and restore Office 365 user accounts

When you use Office 365 PowerShell to delete a user account, the account isn't permanently deleted. You can restore the deleted user account within 30 days.

  • The procedures in this topic require you to connect to Office 365 PowerShell. For instructions, see Connect to Office 365 PowerShell.

  • If you use the Get-MsolUser cmdlet without using the All parameter, only the first 500 accounts are returned.

This section presents the procedures without fanfare or superfluous explanation. If you have questions or want more information, you can read rest of the topic.

To delete a user account, use the following syntax:

Remove-MsolUser -UserPrincipalName <Account>

This example deletes the user account BelindaN@litwareinc.com.

Remove-MsolUser -UserPrincipalName belindan@litwareinc.com

To restore a deleted user account within the 30-day grace period, use the following syntax:

Restore-MsolUser -UserPrincipalName <Account>

This example restores the deleted account BelindaN@litwareinc.com.

Restore-MsolUser -UserPrincipalName BelindaN@litwareinc.com

Notes:

  • To see the list of deleted users that can be restored, run the following command:

    Get-MsolUser -All -ReturnDeletedUsers
    
  • If the user account's original user principal name is used by another account, use the NewUserPrincipalName parameter instead of UserPrincipalName to specify a different user principal name when you restore the user account.

When the following command completes, Belinda Newman’s Office 365 user account will be deleted, and the license assigned to Belinda Newman can then be assigned to someone else.

Remove-MsolUser -UserPrincipalName BelindaN@litwareinc.com

Keep in mind, however, that there’s a little bit of a catch involved here. When you delete a user account that account is not permanently deleted, at least not right away. Instead, the account is transferred to the "recycle bin" for 30 days. At any time during those 30 days you can restore the account by using a command similar to this one:

Restore-MsolUser -UserPrincipalName BelindaN@litwareinc.com

So what’s the catch? Well, suppose Belinda Newman was a licensed user when her account was removed. When the account is restored, Belinda Newman will automatically be re-licensed, assuming that any licenses are available. That isn’t necessarily a bad a thing; after all, that means that you don’t have to go through the process of re-licensing user accounts. Just remember, though that if you restore 10 previously-deleted user accounts you might end up using 10 user licenses that you thought were available for other purposes.

NoteNote:
What if you don’t have 10 licenses available? In that case Office 365 will re-license as many users as it can. Any other restored accounts will be restored in an unlicensed state.

If you don’t like the idea of restored users automatically being licensed, then simply remove the license from each account before you delete that account. For instructions, see Remove licenses from user accounts with Office 365 PowerShell.

For more information about the cmdlets that are used in these procedures, see the following topics:

 
Show: