Query user contact information in Microsoft 365

A tenant in Microsoft 365 was utilising the contact information fields for users to populate auto-signatures and also generate the Org Explorer view in Outlook. Needing to query this information, and a simple CSV output would suffice for starters, proved to be more complex than first thought. You cannot access the fields (e.g. job title, department, phone numbers etc) through the ExchangeOnline PowerShell environment as the info is held within the Active Directory side, and the Azure AD commands have all been deprecated… You, therefore, need to use the Microsoft Graph PowerShell (or at least until Microsoft change their minds again and force everyone to re-write scripts).

You first need to install the Microsoft Graph module with PowerShell (this may take several minutes):

Install-Module Microsoft.Graph -Scope CurrentUser -Repository PSGallery -Force

You can then query the user fields, for example:

Get-MgUser -All | Sort-Objects UserPrincipalName | Select-Object UserPrincipalName,JobTitle,Department,TelephoneNumber,Mobile

However, in classic Microsoft style, although the fields are visible to these commands, the data contained in some of them is not so it just returns a blank! In order to access, for example, the Department data for a user, you need to use the beta version of the Microsoft Graph modules. Once again, you must install (and wait some time):

Install-Module Microsoft.Graph.Beta -Repository PSGallery -Force

You can then use the same query as above, but utilise Get-MgBetaUser as follows:

Get-MgBetaUser -All | Sort Department | Select UserPrincipalName,JobTitle,Department,BusinessPhones,FaxNumber,MobilePhone | Export-Csv -Path “C:\temp\365Users.csv”

As an aside: <rant> the requirement to use the ‘beta’ version to access Department field data has been there for over 2 years (!) at this point; you’d think they would have moved into the full released version by now <rant> Oh, and the office phone field is stored as a string so outputting to CSV loses the information as the export function doesn’t know how to handle it…

 

Related Posts

Need IT Help?

If you need assistance please feel free to reach out to us.