0

Get Microsoft 365 License Usage Count Using PowerShell

Keeping an eye on the available licenses in your Microsoft tenant is essential to ensuring you and your users have what is needed to keep the business running. Whether you assign licenses directly or you use Group Based Licensing, if a user needs a specific license, there shouldn’t be any hiccups when assigning. Today I am going to share a PowerShell script to get Microsoft 365 license usage count using PowerShell and Graph API.

Requirements

In order to query license information for your tenant, you will need the following API Scopes permitted.

  • Microsoft.Graph or Microsoft.Graph.Beta PowerShell modules
  • Directory.Read.All or Organization.Read.All

 

Get Microsoft 365 License Usage Count Using PowerShell

Before we get into the PowerShell script, I wanted to point out that the Microsoft API’s don’t show the friendly display names for these licenses. Instead, they use a SkuPartNumber to give you an idea of what the licenses is regarding. The problem here is that you also won’t find the SkuPartNumber anywhere in the portal so it’s kind of a pain to make sure the license you’re targeting in the API is in fact the license in the Azure portal.
 

Luckily, there is a Microsoft Doc that has this information but it’s not always up to date. The link to that doc is https://learn.microsoft.com/en-us/entra/identity/users/licensing-service-plan-reference. There’s about 400+ Sku’s that are shown so it’s also nice that they have provided a csv file that we can use PowerShell to be able to pull these names into our Script.

$LicenseFile = 'C:\temp\m365license.csv'
$CutoffDate = (Get-Date).AddDays(-7)

if (Test-Path $LicenseFile) {
    $LastWriteTime = Get-ChildItem -Path $LicenseFile | select -ExpandProperty LastWriteTime

    if ($CutoffDate -gt $LastWriteTime) {
        #csv file is older than a week old.  Let us get a newer version
        Invoke-WebRequest -Uri 'https://download.microsoft.com/download/e/3/e/e3e9faf2-f28b-490a-9ada-c6089a1fc5b0/Product%20names%20and%20service%20plan%20identifiers%20for%20licensing.csv' -OutFile C:\temp\m365license.csv
    }
} else {
    #csv file was not found so let us download it now
    Invoke-WebRequest -Uri 'https://download.microsoft.com/download/e/3/e/e3e9faf2-f28b-490a-9ada-c6089a1fc5b0/Product%20names%20and%20service%20plan%20identifiers%20for%20licensing.csv' -OutFile C:\temp\m365license.csv
}

$csvList = Import-Csv C:\temp\m365license.csv
$LicenseHash = @{}

$csvList | ForEach-Object {
    if (-not $LicenseHash[$_.Guid]) {
        $LicenseHash.Add($_.GUID, $_.Product_Display_Name)
    }
}

$LicenseList = Get-MgSubscribedSku

#Uncomment if you only want to display licenses that are maxed out
foreach ($License in $LicenseList) {
    if ($License.PrepaidUnits.Enabled -ge 1) {
        #if ($License.ConsumedUnits -ge $License.PrepaidUnits.Enabled) {
            [PSCustomObject]@{
                LicenseName   = $LicenseHash[$License.SkuId]
                SkuPartNumber = $License.SkuPartNumber
                SkuId         = $License.SkuId
                Remaining     = $License.PrepaidUnits.Enabled - $License.ConsumedUnits
                Enabled       = $License.PrepaidUnits.Enabled
                Used          = $License.ConsumedUnits
            }
        #}
    }
}

Microsoft 365 License Usage Count PowerShell Graph API
 

As you can see from above, the Identity Governance P2 Step Up license has not been updated in the downloadable csv file so the license name shows up blank.

Conclusion

Hopefully this article was able to help you get Microsoft 365 License usage count using PowerShell and Graph API. Sometimes we’re too busy to manually keep an eye on it so having this script along with an email alert would be helpful for preventing your licenses being maxed out.

5/5 - (7 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

Leave a Reply

Your email address will not be published. Required fields are marked *