Script repository

Assign Microsoft 365 licenses after adding to a group

Updated on: Jan 18, 2026, Views: 4148

Microsoft 365

The scripts assign Microsoft 365 licenses after adding to a group. To execute either of the scripts, create a business rule triggering After adding a member to a group.

Assign all licenses enabled in user tenant

In the script, the $locationProperty variable specifies the name of a property to obtain the user location in Microsoft 365 from. The value of the property must be represented by a two-letter country code per ISO 3166-1, for example, US or DE.

$locationProperty = "c" # TODO: modify me

# Bind to group member.
$member = $Context.BindToObjectEx("Adaxes://%member%", $True)
if ($member.Class -ine "user")
{
    return
}

# Get location from specified property.
try
{
    $location = $member.Get($locationProperty)
}
catch
{
    $Context.LogMessage("Location not specified. Microsoft 365 account will not be assigned", "Error")
    return
}

# Get Microsoft 365 properties.
$microsoft365Properties = $member.GetMicrosoft365Properties()

# Set user location.
$microsoft365Properties.Location = $location

# Enable licenses
$licenses = $microsoft365Properties.Licenses

foreach ($license in $licenses)
{
    $license.Assigned = $True
}

# Save changes
$member.SetMicrosoft365Properties($microsoft365Properties)
$member.SetInfo()

Assign specific licenses based on the target group

Parameters

  • $locationProperty - the name of a property to obtain the user location in Microsoft 365 from. The value of the property must be represented by a two-letter country code per ISO 3166-1, for example, US or DE.
  • $groupInfos - maps distinguished names (DNs) of groups with the SKU Part Numbers of the corresponding Microsoft 365 licenses.
$locationProperty = "c" # TODO: modify me
$groupInfos = @{
    "CN=My group 1,OU=Groups,DC=domain,DC=com" = @("ENTERPRISEPREMIUM", "SHAREPOINTSTORAGE")
    "CN=My group 2,OU=Groups,DC=domain,DC=com" = @("ENTERPRISEPACK")
} # TODO: modify me

# Bind to group member.
$member = $Context.BindToObjectEx("Adaxes://%member%", $True)
if ($member.Class -ine "user")
{
    return
}

# Get location from specified property.
try
{
    $location = $member.Get($locationProperty)
}
catch
{
    $Context.LogMessage("Location not specified. Microsoft 365 account will not be assigned", "Error")
    return
}

# Get Microsoft 365 properties.
$microsoft365Properties = $member.GetMicrosoft365Properties()

# Set user location.
$microsoft365Properties.Location = $location

# Get the Microsoft 365 license plans to enable
$planNames = $groupInfos["%distinguishedName%"]
if ($planNames -eq $NULL)
{
    $Context.LogMessage("There are no Microsoft 365 licenses specified for group %name%.", "Error")
    return
}

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

# Save changes
$member.SetMicrosoft365Properties($microsoft365Properties)
$member.SetInfo()

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.