Managing contacts in Microsoft 365 (Azure Active Directory) is an essential task for administrators. While the Microsoft 365 admin center provides an interface to view and manage contacts, using PowerShell provides flexibility and automation, particularly when dealing with large organizations. In this article, we will explore how to retrieve a list of all contacts in your Microsoft 365 tenant using Microsoft Graph PowerShell, displaying important details like the contact’s display name and email in a clean tabular format.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All" "Contacts.Read"
# Query the contacts in Azure AD
$contacts = Get-MgContact -All
# Select relevant properties and output in tabular format
$contacts | Select-Object DisplayName Mail | Format-Table -AutoSize
Connect-MgGraph
cmdlet. The -Scopes
parameter specifies the permissions required for this task: User.Read.All
and Contacts.Read
. These scopes allow the script to read user and contact information from Azure AD.Get-MgContact
cmdlet is then used to query all the contacts in the directory. The -All
parameter ensures that all contacts are retrieved even if there are more than the default 100 returned in a single query.Select-Object
to pick the DisplayName
and Mail
fields, which are the essential properties of a contact. Finally, the Format-Table -AutoSize
cmdlet is used to display the results in a clean tabular format, making it easy to read the contact list.$contacts | Select-Object DisplayName Mail | Export-Csv -Path "TenantContacts.csv" -NoTypeInformation
This will generate a CSV file containing the list of contacts with their display names and email addresses.
$contacts = Get-MgContact -All | Where-Object { $_.Mail -ne $null }
CompanyName
, JobTitle
, or PhoneNumbers
to get more information about the contacts. You can modify the Select-Object
to include these fields:$contacts | Select-Object DisplayName Mail CompanyName JobTitle | Format-Table -AutoSize
Cause: You may not have the required permissions to run the script.
Solution: Ensure that your account has been granted the necessary permissions (User.Read.All
and Contacts.Read
). If you're using application-based authentication, ensure that your app registration has these permissions assigned in Azure AD.
Cause: This error may occur if the Microsoft Graph PowerShell module is not up to date.
Solution: Update your Microsoft Graph module to the latest version using the following command:
Update-Module Microsoft.Graph
Cause: Some contacts may not have a Mail
property.
Solution: You can add a check to filter out contacts without an email address using:
$contacts | Where-Object { $_.Mail -ne $null } | Select-Object DisplayName Mail | Format-Table -AutoSize
Cause: This can happen if there’s an issue with the authentication process when connecting to Microsoft Graph.
Solution: Ensure that you are using the correct credentials and try reconnecting using:
Disconnect-MgGraph
Connect-MgGraph -Scopes "User.Read.All" "Contacts.Read"
Using Microsoft Graph PowerShell to manage and query Azure AD contacts offers a powerful way to automate daily administrative tasks. The script discussed in this article provides a simple yet effective method for retrieving and displaying tenant contacts, while additional enhancements make it adaptable to various scenarios. As with any script, be sure to test it in your environment and address any errors as they arise. Microsoft Graph continues to grow, making it a valuable tool for any administrator seeking efficient management of Microsoft 365.
© m365corner.com. All Rights Reserved. Design by HTML Codex