This guide demonstrates how to use the Get-MgUserContact cmdlet in Microsoft Graph PowerShell to retrieve a user's contacts. Learn how to list all contacts, filter by specific criteria, and export contact details with practical examples.
To retrieve the contacts of a specific user, you can use the Get-MgUserContact cmdlet from the Microsoft Graph PowerShell module. This cmdlet allows you to fetch personal contacts associated with a particular user. Here’s how you can do it:
The Get-MgUserContact cmdlet retrieves a list of contacts from a specified user’s mailbox. You need the user's user principal name (UPN) or user ID to perform this operation.
Get-MgUserContact -UserId <String>
This command retrieves all contacts for the user john.doe@example.com.
Get-MgUserContact -UserId "john.doe@example.com"
This command retrieves contacts for the user john.doe@example.com and selects specific properties to display: DisplayName, EmailAddresses, and BusinessPhones.
Get-MgUserContact -UserId "john.doe@example.com" | Select-Object DisplayName, EmailAddresses, BusinessPhones
To export the contacts of a specific user to a CSV file, you can combine the cmdlet with Export-Csv.
Get-MgUserContact -UserId "john.doe@example.com" | Select-Object DisplayName, EmailAddresses, BusinessPhones | Export-Csv -Path "JohnDoeContacts.csv" -NoTypeInformation
This command retrieves contacts for the user john.doe@example.com, selects specific properties, and exports the result to a CSV file named JohnDoeContacts.csv.
Solution: Ensure the account used to run the cmdlet has the necessary permissions to access the user’s contacts. The account should have the appropriate Microsoft Graph permissions, such as Contacts.Read or Contacts.ReadWrite.
Solution: Verify that the user ID or UPN provided is correct. Ensure the user exists in the Microsoft 365 environment.
Solution: Ensure that the -UserId parameter is not null or empty. Provide a valid user ID or UPN.
1. What is Get-MgUserContact used for?
Get-MgUserContact is a Microsoft Graph PowerShell cmdlet used to retrieve contacts from a user's mailbox, including their display names, email addresses, and phone numbers.
2. Can I filter contacts by their display name?
Yes, use the -Filter parameter to filter by specific properties like display name. Example:
Get-MgUserContact -UserId "<UserPrincipalName>" -Filter "displayName eq 'John Doe'"
3. How can I export contact details to a CSV file?
Use this script to export contact details like display name, email address, and company:
$Contacts = Get-MgUserContact -UserId "<UserPrincipalName>" -All
$Contacts | Select-Object DisplayName, EmailAddresses, CompanyName | Export-Csv -Path "C:\Path\To\UserContacts.csv" -NoTypeInformation
4. What permissions are required to retrieve user contacts?
You need the Contacts.Read or Contacts.ReadWrite permission in Microsoft Graph PowerShell. Ensure appropriate permissions are granted in Azure AD.
The Get-MgUserContact cmdlet is a valuable tool for retrieving personal contacts of specific users in a Microsoft 365 environment. By understanding its syntax, usage examples, and handling potential errors, administrators can efficiently manage user-specific contacts and integrate them with other systems as needed.
Use the Get-MgUserContact cmdlet to enhance your organization's contact management and ensure that your directory is always up-to-date with the latest information.
© m365corner.com. All Rights Reserved. Design by HTML Codex