Delete Microsoft 365 user accounts with PowerShell

You can use PowerShell for Microsoft 365 to delete and restore user accounts.

Note

Learn how to restore a user account by using the Microsoft 365 admin center.

For a list of additional resources, see Manage users and groups.

Use the Azure Active Directory PowerShell for Graph module

First, connect to your Microsoft 365 tenant.

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

Remove-AzureADUser -ObjectID <sign-in name>

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

Remove-AzureADUser -ObjectID fabricec@litwareinc.com

Note

The -ObjectID parameter in the Remove-AzureADUser cmdlet accepts either the account's sign-in 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 Caleb Sills.

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

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

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

Use the Microsoft Azure Active Directory Module for Windows PowerShell

When you delete a user account through the Microsoft Azure Active Directory Module for Windows PowerShell, the account isn't permanently deleted. You can restore the deleted user account within 30 days.

First, connect to your Microsoft 365 tenant.

To delete a user account, use the following syntax:

Remove-MsolUser -UserPrincipalName <sign-in name>

Note

PowerShell Core doesn't support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. Run these cmdlets from Windows PowerShell.

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 <sign-in name>

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

Restore-MsolUser -UserPrincipalName BelindaN@litwareinc.com

Note

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.

See also

Manage Microsoft 365 user accounts, licenses, and groups with PowerShell

Manage Microsoft 365 with PowerShell

Get started with PowerShell for Microsoft 365