Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Get Microsoft 365 License Expiration Dates using Graph Api

December 16, 2023
in Blog, Microsoft Graph, Powershell
0
ADVERTISEMENT

Table of Contents

Microsoft Graph is the gateway to data and intelligence in Microsoft 365. It provides a unified programmability model that you can use to access the tremendous amount of data in Microsoft 365, Windows, and Enterprise Mobility + Security.

In this post, I will show you how to use it to get the license expiration dates of all Microsoft 365 subscriptions in your tenant.

Connect to Graph PowerShell

Pre-requisites

Before you begin, you need to ensure the following:

  • A computer with Windows PowerShell 5.1+ or PowerShell 7+ (Mac or Linux)
  • Installed Microsoft Graph PowerShell SDK.

Or you can open a new PowerShell (Terminal) window then run the below commands to install the Microsoft Graph PowerShell SDK.

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

Once done, connect to Microsoft Graph PowerShell with the following required scopes then sign in using an administrative account or Global admin account.

Connect-MgGraph -Scopes Directory.Read.All

Get Microsoft 365 License Expiration Dates (PowerShell SDK)

Below is a simple script that help you get the license expiration dates of all Microsoft 365 subscriptions in your tenant. You can copy then run the code directly in the PowerShell console or you can create a PowerShell script for later use.

# Caching the information into variables
$skus = Get-MgSubscribedSku -All
$renewalData = Get-MgBetaDirectorySubscription -All
$translationTable = Invoke-RestMethod -Method GET -Uri "https://bonguides.com/ms/skus" | ConvertFrom-Csv

# Create the report with the renewal information
$skuReport = @()
foreach ($sku in $skus) {
    $expireDate = $renewalData | Where-Object {$_.skuId -match $($sku.SkuId)}
    $skuNamePretty = ($translationTable | Where-Object {$_.GUID -eq $sku.skuId} | Sort-Object Product_Display_Name -Unique).Product_Display_Name

    if ($expireDate.nextLifecycleDateTime) {
        $DaysToRenewal = ($expireDate.nextLifecycleDateTime - $((Get-Date).Date)).Days
    } else {
        $DaysToRenewal = $null
    }

    $object = [PSCustomObject][Ordered]@{
        LicenseName   = $skuNamePretty
        SkuPartNumber = $Sku.SkuPartNumber
        SkuId         = $Sku.SkuId
        ActiveUnits   = $Sku.PrepaidUnits.Enabled
        ConsumedUnits = $Sku.ConsumedUnits
        RenewalDate   = $expireDate.nextLifecycleDateTime
        DaysToRenewal = $DaysToRenewal
    }
    $skuReport += $object
}

# Output options to console, graphical grid view or export to CSV file.
$skuReport | Format-Table
# $skuReport | Out-GridView -Title "License Report"
# $skuReport | Export-Csv 'C:\Temp\report.csv' -Nti -Encoding UTF8

In the script, we’ve added some output options such as show it in the graphical grid view or export it to a CSV file. You can uncomment to enable them.

hK51TcCp6GxOJ5tp0DdUHu3M1s9NycmR1BayepMWLilzhQRKfcrlHpLYjB0b
3ULQ74VOPMOOwYulyyNV4Nfs4cg1EIsCK1omJVqEpuktAdK78eTvbKSfTVy9

Get Microsoft 365 License Expiration Dates (REST APIs)

Alternatively, we can use the Microsoft Graph Rest Api to get the license expiration dates. When using this method:

  • We don’t need to install any modules of the Microsoft Graph PowerShell SDK (~ 80 modules).
  • We can do it from any machine with Microsoft PowerShell/ PowerShell Core installed (Linux and macOS). Because it uses the native PowerShell cmdlet Invoke-RestMethod to make a request.
  • Instead of using an account for authentication and authorization, we use the app-only access (access without a user).
Important: Before you begin, make sure you’ve created an app registration in Microsoft entra admin center and collect some required information such as clientId, tenantId and the clientSecret.

Once the app has been created, replace your app’s information (clientId, tenantId and the clientSecret) into the below code. Steps in this code:

  1. Get access token ($token) with app registration client secret.
  2. Create the request header ($headers) for API call.
  3. Create requests to the Microsoft Graph resource ($uri) with pagination to get all data.
  4. Build the report ($result) from API call response.
  5. Output options to console, graphical grid view or export to CSV file.
# Get access token with app registration client secret.
$clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"

$body = @{    
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    client_Id     = $clientId
    Client_Secret = $clientSecret
} 

$tokenRequest = Invoke-RestMethod -Uri $uri -Method POST -Body $body
$token = $tokenRequest.access_token

# Create the request header for API call.
$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type"  = "application/json"
}

# Caching the information into variables
$subscribedUri = 'https://graph.microsoft.com/beta/subscribedSkus'
$skus = (Invoke-RestMethod -Method GET -Headers $headers -Uri $subscribedUri).Value

# Get the renewal data
$uri = 'https://graph.microsoft.com/beta/directory/subscriptions'
$renewalData = (Invoke-RestMethod -Method GET -Headers $headers -Uri $uri).Value

# Create the report with the renewal information and translate SkuPartNumber to license name.
$SkuReport = @()
$translationTable = Invoke-RestMethod -Method GET -Uri "https://bonguides.com/ms/skus" | ConvertFrom-Csv

foreach ($sku in $skus) {
    $expireDate = ($renewalData | Where-Object {$_.skuId -match $($sku.SkuId)}).nextLifecycleDateTime
    $skuNamePretty = ($translationTable | Where-Object {$_.GUID -eq $sku.skuId} | Sort-Object Product_Display_Name -Unique).Product_Display_Name

    if ($expireDate) {
        $DaysToRenewal = ([Datetime]::ParseExact($expireDate, 'yyyy-MM-ddTHH:mm:ssZ', $null) - $((Get-Date).Date)).Days
        $RenewalDate = [Datetime]::ParseExact($expireDate, 'yyyy-MM-ddTHH:mm:ssZ', $null)
    } else {
        $DaysToRenewal = $null
        $expireDate = $null
    }

    $object = [PSCustomObject][Ordered]@{
        LicenseName   = $skuNamePretty
        SkuPartNumber = $Sku.SkuPartNumber
        SkuId         = $Sku.SkuId
        ActiveUnits   = $Sku.PrepaidUnits.Enabled
        ConsumedUnits = $Sku.ConsumedUnits
        RenewalDate   = $RenewalDate
        DaysToRenewal = $DaysToRenewal
    }
    $SkuReport += $object
}
$SkuReport | Format-Table
$SkuReport | Out-GridView -Title "License Report"

Manage Microsoft 365 Using Microsoft Graph

You can also take a look at the following Microsoft Graph posts that help to manage Microsoft 365 efficiently.

  • Create a new user
  • Create bulk users in Microsoft 365
  • Get a list of all users in Microsoft 365
  • Update user properties
  • Add a user to a group
  • Add bulk users to a group
  • Remove users from a group
  • Remove multiple users from a group
  • Assign managers for Microsoft 365 users
  • Assign licenses to users
  • Removing licenses from user accounts
  • Delete a user from Microsoft 365
  • How to use Get-MgUser cmdlet
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

Translate Microsoft 365 License GUIDs to Product Names in PowerShell Microsoft Graph

Next Post

Converting DateTime Obtained from Microsoft Graph Call to PowerShell Date and Time Format

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory