The Remove-MgUserContact cmdlet is a powerful tool within the Graph PowerShell module that allows administrators to delete user contacts from their Microsoft 365 environment. This article delves into the cmdlet's syntax, provides practical usage examples, offers tips and solutions for potential errors, and outlines key use cases.
The basic syntax for the Remove-MgUserContact cmdlet is as follows:
Remove-MgUserContact -UserId <String> -ContactId <String> [<CommonParameters>]
Remove-MgUserContact -UserId "john.doe@contoso.com" -ContactId "AAMkADkzZjk3ODk3LTgxMjQtNDI1NC1hYzBiLWZhM2Y4MTY1NzNlYwBGAAAAAABBbbQxofVsQqcQEAAAAAAA="
In this example, a specific contact for user "john.doe@contoso.com" is removed using the contact's unique ID.
Remove-MgUserContact -UserId "jane.doe@contoso.com" -ContactId "AAMkADkzZjk3ODk3LTgxMjQtNDI1NC1hYzBiLWZhM2Y4MTY1NzNlYwBGAAAAAABBbbQxofVsQqcQEAAAAAAA=" -Verbose
Using the -Verbose parameter provides detailed output useful for troubleshooting and confirming the contact removal process.
$contactsToRemove = @("ContactId1", "ContactId2", "ContactId3")
$userId = "john.doe@contoso.com"
foreach ($contactId in $contactsToRemove) {
Remove-MgUserContact -UserId $userId -ContactId $contactId
}
This example demonstrates how to remove multiple contacts for a single user by iterating over an array of contact IDs.
To obtain the contact ID for the contact to be deleted, use the Get-MgUserContact cmdlet. This cmdlet retrieves the list of contacts for a specified user, allowing you to identify and select the contact IDs needed for removal.
Get-MgUserContact -UserId "john.doe@contoso.com"
No. Once a contact is deleted using Remove-MgUserContact, it is permanently removed. There’s no restore option in Graph PowerShell for contacts.
Remove-MgUserContact deletes contacts from a user’s personal Outlook mailbox, not from the organization’s Global Address List.
Yes. You can retrieve a list of contacts with Get-MgUserContact and pipe the results into Remove-MgUserContact in a loop for bulk deletions.
You need the Contacts.ReadWrite permission (delegated or application-based) in Microsoft Graph to delete user contacts successfully.
| Error | Cause | Solution |
|---|---|---|
| "Resource Not Found" | The specified contact ID does not exist or the user ID is incorrect. | Verify the contact ID and user ID using Get-MgUserContact to ensure accuracy. |
| "Insufficient Permissions" | The executing account lacks the necessary permissions to delete contacts. | Ensure the account has the Contact.ReadWrite permission. Use the following script to check and grant the required permission: |
Remove-MgUserContact cmdlet deletes only personal or external contacts stored in a user’s mailbox, not actual Microsoft 365 tenant users.Remove-MgUser cmdlet instead.
Get-MgUserContact before executing deletion commands — especially when running scripts in bulk.
The Remove-MgUserContact cmdlet is an essential tool for administrators managing Microsoft 365 environments. It ensures the removal of unnecessary or outdated contacts, contributing to better organization and security. By understanding the cmdlet's syntax, exploring practical examples, and addressing potential errors, administrators can effectively use this cmdlet to maintain clean and efficient contact lists.
For more information and detailed documentation, visit the official Microsoft documentation.
Note: Microsoft Graph API allows you to delete user contacts via the /users/{userId}/contacts/{contactId} endpoint. Below are examples using Invoke-MgGraphRequest.
# Replace with actual UserId (UPN or ID) and ContactId
$userId = "john.doe@contoso.com"
$contactId = "AAMkADkzZjk3ODk3LTgxMjQtNDI1NC1hYzBiLWZhM2Y4MTY1NzNlYwBGAAAAAABBbbQxofVsQqcQEAAAAA"
$uri = "https://graph.microsoft.com/v1.0/users/$userId/contacts/$contactId"
Invoke-MgGraphRequest -Method DELETE -Uri $uri
✅ Equivalent to: Remove-MgUserContact -UserId "john.doe@contoso.com" -ContactId "<id>"
# Replace with actual UserId and contact IDs
$userId = "john.doe@contoso.com"
$contactsToRemove = @(
"AAMkADkzZjk3ODk3LTgxMjQtNDI1NC1hYzBiLWZhM2Y4MTY1NzNlYwBGAAAAAA1",
"AAMkADkzZjk3ODk3-LTgxMjQtNDI1LC0yYzBiLWZhM2Y4MTY1NzNlYwBGAAAAAA2",
"AAMkADkzZjk3ODk3LTgxMjQtNDI1LC0xYzBiLWZhM2Y4MTY1NzNlYwBGAAAAAA3"
)
foreach ($contactId in $contactsToRemove) {
$uri = "https://graph.microsoft.com/v1.0/users/$userId/contacts/$contactId"
Invoke-MgGraphRequest -Method DELETE -Uri $uri
}
✅ Equivalent to using a looped version of Remove-MgUserContact Ideal for administrators needing to clean up contact records in batch.
You’ll need one of the following permissions:
Contacts.ReadWriteContacts.ReadWrite© m365corner.com. All Rights Reserved. Design by HTML Codex