Delete Microsoft 365 User Using Graph PowerShell

This article explains how you can delete Microsoft 365 users using Graph PowerShell with the help of Remove-MgUser cmdlet.


Prerequisites

  • You need to install the Microsoft Graph PowerShell SDK. Install-Module Microsoft.Graph -Scope CurrentUser is the command.
  • You need to connect to the Microsoft Graph PowerShell Module with the necessary permissions. Connect-MgGraph -Scopes "User.ReadWrite.All" is the command.

Deleting Single M365 User

To delete a single user, you can pass in the UserPrincipalName of the user to be deleted as the parameter to -UserId. Alternatively, you can also pass in the UserId. Get-MgUser is the cmdlet that helps you get the User IDs.

# Ensure you are connected to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
                                
# Define the user principal name of the user you want to delete
$userPrincipalName = "user@example.com"
                                
# Soft delete the user
Remove-MgUser -UserId $userPrincipalName 
                                
Write-Output "User $userPrincipalName has been soft deleted."

Deleting Multiple M365 Users

You can delete multiple users contained in a PowerShell array by looping through the array items [users] and executing the Remove-MgUser cmdlet for each of the items within the array.

# Define the array of user principal names
$users = @("testuser2@7xh7fj.onmicrosoft.com", "testuser3@7xh7fj.onmicrosoft.com")
                                
# Loop through each user and delete
foreach ($user in $users) {
        try {
             Remove-MgUser -UserId $user 
             Write-Output "deleted user: $user"
        } catch {
             Write-Output "Failed to delete user: $user. Error: $_"
        }
}

Deleting M365 Users from CSV File

You can also import users from CSV file, loop through them, and execute Remove-MgUser cmdlet for each of the users so that they get deleted.Your CSV file should contain either the UserId or UserPrincipalName as the CSV header and the corresponding values.

# Import required module
Import-Module Microsoft.Graph.Identity.DirectoryManagement
                                
# Get the list of users from a CSV file
$users = Import-Csv -Path "C:\path\to\your\file.csv"
                                
# Loop through each user and soft delete
foreach ($user in $users) {
        Remove-MgUser -UserId $user.UserPrincipalName -Force
        Write-Output "Soft deleted user: $($user.UserPrincipalName)"
}

Further Enhancing the Script

Enhancing the script for deleting Microsoft 365 groups based on a specific creation date can be approached from several angles, focusing on adding more functionality, improving user experience, and ensuring better safety and compliance. Here are some suggestions:

  • Error Handling: Adding comprehensive error handling will make the script more reliable, especially in environments with many users. You can catch errors and log them without stopping the script.
  • Graph PowerShell Script for deleting Microsoft 365 Group based on created date full script.
  • Logging Mechanism: Implement a logging mechanism to keep track of which users have been deleted and any errors that might have occurred. This is useful for audits and troubleshooting.
  • Graph PowerShell Script for deleting Microsoft 365 Group based on created date full script.
  • Confirmation and User Review: Before performing deletions, you could add a step for reviewing and confirming the list of users to be deleted. This can prevent accidental deletions.
  • Graph PowerShell Script for deleting Microsoft 365 Group based on created date full script.
  • Parallel Processing: For very large datasets, consider using parallel processing to speed up the deletions. PowerShell 7 introduces the ForEach-Object -Parallel which can be utilized here.
  • Integration with Notification Systems: Integrate notifications (e.g., email alerts) to inform administrators about the completion of the deletion process or any critical errors that may need immediate attention.

These enhancements can help ensure that the script is not only effective at deleting user accounts but also compliant with organizational policies and capable of providing detailed logs for forensic or compliance purposes.


Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell
Import M365 Users to Microsoft Teams from CSV file
Get Microsoft 365 Group Owners List Using Graph PowerShell
Get Microsoft 365 Group Members Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex