To delete an orphaned user account in Microsoft Entra (formerly Azure AD) that was created through directory synchronization, you can use the Microsoft Graph PowerShell module. Here are the steps you should follow:
- Ensure you have the necessary modules installed: Make sure that the Microsoft Graph PowerShell module is installed.
- Connect to Microsoft Graph: You need to connect to your Microsoft 365 tenant using the appropriate permissions. Use the following command:
Connect-MgGraph -Scopes User.Read.All, User.ReadWrite.All - Identify the orphaned user: You can find the orphaned user by using the
Get-MgUsercmdlet with a filter for the user's principal name or display name. For example:$user = Get-MgUser -Filter "userPrincipalName eq '******@contoso.com'" - Remove the orphaned user: Once you have identified the user, you can remove them using the
Remove-MgUsercmdlet:Remove-MgUser -UserId $user.id
If you encounter any issues with permissions or if the command is blocked, ensure that you have the necessary permissions assigned to your account to delete users in Microsoft Entra.
References: