Script repository

Check if number of unused Microsoft 365 licenses is below limit

Updated on: Jan 18, 2026, Views: 2859

Microsoft 365

The script returns true if the number of Microsoft 365 licenses is below limit. To execute the script, use the If PowerShell script returns true condition in a business rule, custom command or scheduled task.

Parameters

  • $limit - the minimum number of unused licenses. The script will return $true if the number of available licenses is below this limit.
  • $skus - the SKU Part Numbers of the licenses to check. If set to $null, the script checks all enabled licenses.
$limit = 5 # TODO: modify me
$skus = @("ENTERPRISEPREMIUM") # TODO: modify me

$Context.ConditionIsMet = $False
$tenantDN = "%adm-AssociatedO365Tenant%"
if ([System.String]::IsNullOrEmpty($tenantDN))
{
    $Context.LogMessage("No Microsoft 365 tenant is associated with the user", "Error")
    return
}

# Bind to Microsoft 365 tenant.
$tenant = $Context.BindToObjectByDN($tenantDN)

# Check licenses
foreach ($sku in $tenant.Skus)
{
    if (($skus -ne $NULL) -and ($skus -notcontains $sku.SkuPartNumber))
    {
        continue
    }
    
    $difference = $sku.TotalUnits - $sku.ConsumedUnits
    if ($difference -gt $limit)
    {
        continue
    }
    
    $Context.ConditionIsMet = $True
}

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.