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.
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).
Once the app has been created, replace your app’s information (clientId, tenantId and the clientSecret) into the below code. Steps in this code:
- Get access token ($token) with app registration client secret.
- Create the request header ($headers) for API call.
- Create requests to the Microsoft Graph resource ($uri) with pagination to get all data.
- Build the report ($result) from API call response.
- 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
Not a reader? Watch this related video tutorial: