Script repository

Cancel user creation if there are no available Microsoft 365 licenses

Updated on: Jan 18, 2026, Views: 3578

Microsoft 365, Provisioning

The script cancels creation of a new user if the number of available Microsoft 365 licenses in tenant associated with the user is below limit. To execute the script, create a business rule triggering Before creating a user.

Parameter

  • $limit - the minimum number of licenses that must be available.
  • $skuPartNumbers - the SKU Part Numbers of the Microsoft 365 licenses to check. If set to an empty array (i.e. @()), the check will be performed for all licenses.
$limit = 2 # TODO: modify me
$skuPartNumbers = @("ENTERPRISEPACK", "OFFICESUBSCRIPTION") # TODO: modify me

if ([System.String]::IsNullOrEmpty("%adm-AssociatedO365Tenant%"))
{
    return
}
$tenant = $Context.BindToObjectByDN("%adm-AssociatedO365Tenant%")

# Check available licenses.
$skuPartNumbers = New-Object "System.Collections.Generic.HashSet[System.String]" (,[string[]]$skuPartNumbers)
foreach ($sku in $tenant.Skus)
{
    if (($skuPartNumbers.Count -ne 0) -and (-not($skuPartNumbers.Contains($sku.SkuPartNumber))))
    {
        continue
    }
    
    $difference = $sku.TotalUnits - $sku.ConsumedUnits
    if ($sku.Enabled -and ($difference -lt $limit))
    {
        # Cancel user creation.
		    $Context.Cancel("The number of Microsoft 365 licenses is insufficient. User will not be created.")
        return
    }
}

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.