Script repository

Assign Microsoft 365 license based on availability

Updated on: Jan 18, 2026, Views: 8464

Microsoft 365

The script assigns Microsoft 365 licenses based on their availability. For example, you can try assigning the Enterprise E5 license, and if there are no available E5 licenses, assign an Enterprise E3 one. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

Parameters

  • $locationProperty - the name of the user property whose value will be used as the user location in Microsoft 365. The value of the property must be represented by a two-letter country code per ISO 3166-1 (e.g. US or DE). Locations of existing Microsoft 365 accounts will remain unchanged.
  • $licenseSKUs - the SKU Part Numbers of the Microsoft 365 licenses that should be assigned to the user. The script will attempt assigning the licenses exactly in the order you specify. The first license plan in the list that has at least 1 free license will be assigned to a user, and the rest of the list will be ignored.
$locationProperty = "c" # TODO: modify me
$licenseSKUs = @("ENTERPRISEPACK", "ENTERPRISEPREMIUM") # TODO: modify me

# Get associated tenant DN.
$associatedTenantDN = $Context.TargetObject.AssociatedTenantDN
if ([System.String]::IsNullOrEmpty($associatedTenantDN))
{
    $Context.LogMessage("No associated tenant", "Warning")
    return
}

# Bind to tenant.
$tenant = $Context.BindToObjectByDN($associatedTenantDN)

$skuToAssign = $NULL
foreach ($skuPartNumber in $licenseSKUs)
{
    # Get SKU
    $sku = $tenant.Skus | Where{$_.SkuPartNumber -eq $skuPartNumber}
    if ($sku -eq $NULL)
    {
        continue
    }
    
    # Check available licenses.
    if ($sku.TotalUnits -eq $sku.ConsumedUnits)
    {
        continue
    }

    $skuToAssign = $skuPartNumber
    break
}

if ([System.String]::IsNullOrEmpty($skuToAssign))
{
    $Context.LogMessage("No licenses available", "Warning")
    return
}

# Get Microsoft 365 properties
$microsoft365Properties = $Context.TargetObject.GetMicrosoft365Properties()
if ([System.String]::IsNullOrEmpty($microsoft365Properties.Location))
{
    # Get location from the specified property
    try
    {
        $location = $Context.TargetObject.Get($locationProperty)
    }
    catch
    {
        $Context.LogMessage("Location not specified. Microsoft 365 account will not be activated", "Warning")
        return
    }
    
    # Set location
    $microsoft365Properties.Location = $location
}

# Enable licenses
$licenses = $microsoft365Properties.Licenses
foreach ($license in $licenses)
{
    if ($license.Sku.SkuPartNumber -ne $skuToAssign)
    {
        continue
    }
    $license.Assigned = $True
}

# Save changes
$Context.TargetObject.SetMicrosoft365Properties($microsoft365Properties)
$Context.TargetObject.SetInfo()

Comments 2

You must be signed in to comment.

  • Andrew Baker

    Andrew Baker

    Could this be adapted to assign another license if there are none available?

    We are currently in a unique position where we have M365 E3/E5 seperated out into its separate skus and we now have had to purchase more of these licenses as we have more staff and they are combined.

    So basically the question is, can we add a overflow license if no Office 365 E5 is available and then if no license is available assign an M365 E5 license?

    • Support

      Support

      Hello Andrew,

      As it is mentioned in the script description, the $licenseSKUs variable specifies the SKU Part Numbers of the licenses that should be assigned to the user. The script will attempt assigning the licenses exactly in the order you specify. The first license plan in the list that has at least one free license will be assigned to a user, and the rest of the list will be ignored. As such, all you need to do is populate the $licenseSKUs variable with the SKUs you need in the corresponding order.

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.