Exporting M365 User Data with PowerShell on macOS
A Guide to Using Microsoft Graph
In a Microsoft 365 (M365) environment, managing user data effectively is crucial. This blog post guides IT professionals and administrators on macOS through a streamlined approach to exporting comprehensive M365 user data, encompassing both active and inactive accounts, as a CSV file. The method leverages the Microsoft Graph module within PowerShell!!
Installation
When I attempted to install the Graph module using PowerShell, I received a message stating that a Graph Module already exists. So, I tried to connect to it using the following command:
Connect-MGGraph
After various such 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