Delete and restore user accounts with Office 365 PowerShell

Summary: 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.

Before you begin

  • 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.

Use Office 365 PowerShell to block access to individual user accounts

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.

Use the Azure Active Directory V2 PowerShell module to remove a user account

To use the Remove-AzureADUser cmdlet from the Azure Active Directory V2 PowerShell module, you must first connect to your subscription. For the instructions, see Connect with the Azure Active Directory V2 PowerShell module.

After you have connected, use the following syntax to remove an individual user account:

Remove-AzureADUser -ObjectID <Account>

This example removes the user account fabricec@litwareinc.com.

Remove-AzureADUser -ObjectID fabricec@litwareinc.com

Note

The -ObjectID parameter in the Remove-AzureAD cmdlet accepts either the account name, also known as the User Principal Name, or the account's object ID.

To display the account name based on the user's name, use the following commands:

$userName="<User name>"
Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName

This example displays the account name for the user named Caleb Sills.

$userName="Caleb Sills"
Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName

To remove an account based on the user's name, use the following commands:

$userName="<User name>"
Remove-AzureADUser -ObjectID (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName

See also

See these additional topics about managing users with Office 365 PowerShell:

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