Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitLearn 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.| Error | Cause | Solution |
| Error: Insufficient Permissions | 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. |
| Error: User Not Found | 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: |
$Users = Import-Csv -Path "C:\Path\To\File.csv"
foreach ($User in $Users) {
Remove-MgUser -UserId (Get-MgUser -UserPrincipalName $User.UserPrincipalName).Id -Force
}
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.#Soft-delete the user
Remove-MgUser -UserId "user@domain.com"# Permanently delete the user
Remove-MgDirectoryDeletedItem -DirectoryObjectId "user_object_id"Remove-MgUser, the user is moved to the Azure AD Recycle Bin in a soft-deleted state.Remove-MgUser only when you're sure the account is no longer needed.Update-MgUser -BodyParameter @{ AccountEnabled = $false } instead.
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.
Note: To delete a user from Microsoft 365 using Graph API, you must send a DELETE request to /users/{id}. Only users (not guests or contacts) can be deleted via this endpoint.
Remove a Single User
# Replace with the actual UPN or user ID (GUID)
$userId = "john.doe@yourtenant.onmicrosoft.com"
# Send the DELETE request
Invoke-MgGraphRequest -Method DELETE -Uri "https://graph.microsoft.com/v1.0/users/$userId"
✅ You can also pass a user GUID instead of UPN — both are supported.
Remove Multiple Users from CSV
# Sample CSV headers: userPrincipalName
$csvPath = "C:\Users\admin\Documents\remove-users.csv"
$users = Import-Csv -Path $csvPath
foreach ($user in $users) {
$uri = "https://graph.microsoft.com/v1.0/users/$($user.userPrincipalName)"
Invoke-MgGraphRequest -Method DELETE -Uri $uri
}
CSV Format Example
userPrincipalName
john.doe@yourtenant.onmicrosoft.com
jane.admin@yourtenant.onmicrosoft.com
Required Permissions
You need one of the following Graph API permissions:
User.ReadWrite.AllDirectory.AccessAsUser.All (delegated)Directory.ReadWrite.All (application)Graph API Documentation
👉 DELETE /users/{id} - Microsoft Graph v1.0
© m365corner.com. All Rights Reserved. Design by HTML Codex