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

Model After various such unsuccessful attempts

Model

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

Model

Getting user information with Graph

Connect-MGGraph -scopes "User.Read.All"

Model 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/

  1. Navigate to Enterprise applications
  2. All Applications
  3. Remove all the filters
  4. Search for the appId from the above list

    Model

  5. 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 Mail 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[]