Learn how to use Remove-MgUser cmdlet in Microsoft Graph PowerShell to delete users from your Microsoft 365 tenant. This guide covers single and bulk user deletion, troubleshooting common errors, and best practices for user account cleanup.
The Remove-MgUser cmdlet is a powerful tool for administrators managing Microsoft 365 environments. This cmdlet allows you to remove user accounts from your Azure Active Directory (AAD). In this article, we'll cover the prerequisites for using the Remove-MgUser cmdlet, explain its syntax and parameters, provide various usage examples, offer some tips, and address common errors and their solutions.
Install-Module Microsoft.Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
Remove-MgUser -UserId <String> [-WhatIf] [-Confirm] [<CommonParameters>]
-UserId:
(Required) The unique identifier (ID) or User Principal Name (UPN) of the user to be removed.-WhatIf:
(Optional) Shows what would happen if the cmdlet runs. The cmdlet is not executed.-Confirm:
(Optional) Prompts for confirmation before executing the cmdlet.Passing UserPrincipalName to -UserId parameter to delete the user.
Remove-MgUser -UserId "john.doe@contoso.com"
Passing UserId to -UserId parameter to delete the user.
Remove-MgUser -UserId "12345abc-6789-def0-1234-56789abcdef0"
Using -Confirm parameter to confirm the user deletion operation before the user gets deleted.
Remove-MgUser -UserId "jane.doe@contoso.com" -Confirm
Using -WhatIf parameter to preview user deletion operation before the user gets deleted.
Remove-MgUser -UserId "jane.doe@contoso.com" -WhatIf
Using Import-CSV cmdlet to import users from CSV file and delete them. The CSV file should contain UserPrincipalName header with corresponding userprincipalname values.
$users = Import-Csv "C:\Path\To\Users.csv"
foreach ($user in $users) {
Remove-MgUser -UserId $user.UserPrincipalName
}
-WhatIf
Parameter: Before executing the cmdlet, use the -WhatIf
parameter to see what changes will be made without actually removing the user.Issue: Authorization_RequestDenied
Solution: Ensure you have the necessary permissions to remove users. Verify you are signed in with an account that has the User.ReadWrite.All scope and is a Global Administrator or User Administrator in Azure AD.
Issue: Resource 'user_id' does not exist or one of its queried reference-property objects are not present.
Solution: Verify the UserId is correct. Use the Get-MgUser cmdlet to confirm the user exists:
Get-MgUser -UserId "john.doe@contoso.com"
What is Remove-MgUser used for?
Remove-MgUser is a Microsoft Graph PowerShell cmdlet used to delete user accounts from a Microsoft 365 tenant. It is commonly used to manage and clean up inactive or unnecessary accounts.
How can I delete multiple users at once using Remove-MgUser?
Prepare a CSV file with a column for UserPrincipalName or ObjectId and use this script for bulk deletion:
$Users = Import-Csv -Path "C:\Path\To\File.csv"
foreach ($User in $Users) {
Remove-MgUser -UserId (Get-MgUser -UserPrincipalName $User.UserPrincipalName).Id -Force
}
What permissions are required to use Remove-MgUser?
To use Remove-MgUser, the account must have one of the following permissions:
How can I verify that a user has been successfully deleted?
After executing the Remove-MgUser cmdlet, you can confirm the deletion by attempting to retrieve the user's details:
Get-MgUser -UserId "user@domain.com"
If the user has been successfully deleted, this command will return an error indicating that the resource does not exist.
Can I restore a user after deletion, and what is the timeframe for restoration?
Yes, when a user is deleted using the Remove-MgUser cmdlet, the account is moved to a temporary container (soft-deleted) and can be restored within 30 days. To restore a deleted user, use the Restore-MgUser cmdlet
Restore-MgUser -UserId "user@domain.com"
After 30 days, if not restored, the user object is permanently deleted, and their assigned resources are freed.
How can I handle errors indicating insufficient permissions when using Remove-MgUser?
If you encounter an "Insufficient privileges to complete the operation" error, it indicates that your account lacks the necessary permissions to delete users. Ensure that you are signed in with an account that has the User.ReadWrite.All scope and is assigned either the Global Administrator or User Administrator role in Azure Active Directory.
Is it possible to permanently delete a user immediately without waiting for the 30-day retention period?
Yes, to permanently delete a user without waiting for the 30-day retention period, you must first soft-delete the user using Remove-MgUser and then permanently delete the user from the deleted items container:
#Soft-delete the user
Remove-MgUser -UserId "user@domain.com"
Remove-MgUser -UserId "user@domain.com"
# Permanently delete the user
Remove-MgDirectoryDeletedItem -DirectoryObjectId "user_object_id"
The Remove-MgUser cmdlet is essential for managing user accounts in Azure AD. By understanding its syntax, parameters, and usage, you can effectively remove user accounts as needed. Always ensure you have the proper permissions and use caution, especially with the -Confirm
parameter, to avoid unintentional deletions. For bulk operations, consider using scripts to streamline the process. With these guidelines, you'll be well-equipped to manage user removals in your Microsoft 365 environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex