Exporting M365 User Data with PowerShell on macOS
A Guide to Using Microsoft Graph
User data is an important aspect to deal with in any organization using Microsoft 365 (M365). In this blog, I will explain step by step to IT experts and administrators from macOS how to export complete M365 user data, that includes both active and inactive accounts, in a streamlined manner – all consolidated in a CSV file. The approach utilizes the Microsoft Graph module in PowerShell!!
Installation
When I was trying to install the Graph module on PowerShell, I was greeted with the following notification: A Graph Module already exists. Therefore, I tried connecting to it with the command
Connect-MGGraph
After a number of these unsuccessful attempts
I tried to reinstall the MSGraph module that too with admin
Sudo pwsh Install-Module Microsoft.Graph -scope Allusers -Force
Once the download and installation is succesful , the MGGraph successfully connected
Getting user information with Graph
Connect-MGGraph -scopes "User.Read.All"
This requests the scope User.Read.All to the application with id 14d82eec-204b-4c2f-b78-296a70dab67e , if the permission has already granted you should skip the next step
App Registration
Navigate to the app registrations via Identity portal using https://entra.microsoft.com/
- Navigate to Enterprise applications
- All Applications
- Remove all the filters
-
Search for the appId from the above list
- Grant Admin approval if its not already given
Script Execution
$dataTable = Get-MGUser | Select-Object @{Name="Display Name"; Expression={$_.displayName}}, mail, userPrincipalName,Id,BusinessPhones
$dataTable | Export-Csv -Path "Documents/users.csv" -NoTypeInformation
The output would look as below
Display Name | UserPrincipalName | Id | Business Phones | |
---|---|---|---|---|
Adele Vance | AdeleV@5ksww1.onmicrosoft.com | AdeleV@5ksww1.onmicrosoft.com | a7bac51e-93a9-4492-9074-492379207395 | System.String[] |
Alex Wilber | AlexW@5ksww1.onmicrosoft.com | AlexW@5ksww1.onmicrosoft.com | a5aaf665-0090-4457-a92e-8670a610fd1a | System.String[] |
Diego Siciliani | DiegoS@5ksww1.onmicrosoft.com | DiegoS@5ksww1.onmicrosoft.com | cea26297-a7d6-4800-a4b6-25842463b2od | System.String[] |
Liked this post ? You can buy me a coffee