Export All Office 365 Users and Licenses with Microsoft Graph PowerShell

In this tutorial, I am going to show you how you can use a Microsoft Graph PowerShell script to export a list of all your users and their license details to a CSV file.

This is useful as currently, the Microsoft 365 admin portal does not provide the functionality to perform this task. Where as you can achieve the desired results with this script. 


To be able to run the script to export all users you will need to have the Microsoft Graph PowerShell module installed. See my tutorial here for how to install the Microsoft Graph PowerShell module.

You should also have access to a global administrator account to delegate the required permissions to Microsoft Graph PowerShell when connecting.

Export all Office 365 users and license details using Microsoft Graph PowerShell

The script utilises the Get-MgUser cmdlet, which can be found in the Microsoft.Graph.Users PowerShell module and can be called with the User.Read.All permission. To learn more about the Get-MgUser cmdlet, check out my tutorial: How To Use Get-MgUser with Microsoft Graph PowerShell

To create the report including all users and their licenses, follow the below steps:

1. Open up a text editor.

2. Copy and paste the below code into your text editor.

3. Edit the final line of code to change the location of the report: “C:\temp\licensedusers.csv”.

4. Copy and paste the complete script directly into PowerShell.

5. When prompted, sign in to the interactive login prompt with your global administrator credentials and accept the delegated permissions.

Import-Module Microsoft.Graph.Users, Microsoft.Graph.Identity.DirectoryManagement
Connect-MgGraph -Scope User.read.all, DeviceManagementConfiguration.Read.All

$CompanyName = (Get-MgOrganization | Select VerifiedDomains).verifieddomains | Where-Object {$_.IsDefault -eq "True"}
$Users = Get-MgUser -all
$Report = [System.Collections.Generic.List[Object]]::new()
Write-host "Found $($users.count) users for $($CompanyName.Name)" -ForegroundColor Cyan
ForEach ($user in $Users) {
    Write-Host "Retrieving license info for $($User.DisplayName)" -ForegroundColor yellow
    If (Get-MgUserLicenseDetail -USerId $User.id) {
    $licenses = $null
    $licenses = (Get-MgUserLicenseDetail -UserId $User.id).SkuPartNumber -join ", "
    Write-Host "Licenses found for $($User.DisplayName): $licenses" -ForegroundColor cyan
    $obj = [pscustomobject][ordered]@{
            DisplayName       = $user.DisplayName
            UserPrincipalName = $user.UserPrincipalName
            Licenses          = $licenses
    } else {
    Write-Host "No licenses found for $($User.DisplayName)" -ForegroundColor Red
$report | Export-CSV "C:\temp\licensedusers.csv" -NoTypeInformation

Once you have run the script you should find the .csv file located in the area you defined in the script. If you left the script as default, you will find it in C:\temp.


Your user report will look like the following once opened:

Licensed user report
Licensed user report

Daniel Bradley

My name is Daniel Bradley and I work with Microsoft 365 and Azure as an Engineer and Consultant. I enjoy writing technical content for you and engaging with the community. All opinions are my own.

This Post Has 3 Comments

  1. Michele

    Hi, to add the user’s company and job title fields, what should I add in the script?

    1. Daniel

      You can use this section of code instead: 🙂

      $obj = [pscustomobject][ordered]@{
      DisplayName = $user.DisplayName
      UserPrincipalName = $user.UserPrincipalName
      JobTitle = $user.JobTitle
      Licenses = $licenses

  2. Michele

    Thank you; I would also like to add the company field; I tried to add

    Company = $user.CompanyName

    but it does not work

Leave a Reply