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.

Script Repository

Microsoft 365 licenses assigned to users

December 29, 2022 Views: 811

The script generates a report containing users assigned the specified Microsoft 365 licenses. To execute the script, create a report with the corresponding custom column. The report should have a scope configured. In the report, Microsoft 365 licenses will be identified using their display names specified in the Microsoft 365 tenant settings.

Parameters:

  • $licensesColumnID - Specifies the identifier of the custom column that will store the assigned licenses. The column should be of the Text type. To get the identifier of a custom column:
    1. In the Report-specific columns section, on the Columns tab, right-click the custom column.
    2. In the context menu, navigate to Copy and click Column ID.
    3. The column identifier will be copied to clipboard.
  • $licensesParameterName - Specifies the name of the Checkbox list parameter used to select the licenses for report generation with the param- prefix. The values for selected items must be set to the SKU part numbers of the licenses.
  • $separator - Specifies the separator entered in the settings of the Checkbox list parameter for selecting licenses.
Edit Remove
PowerShell
$licensesColumnID = "{6e2ae115-7c27-4b52-a2d4-5e9d943edc92}" # TODO: modify me
$licensesParameterName = "param-Licenses" # TODO: modify me
$separator = ";" # TODO: modify me

# Get licenses to check
$licensesToCheck = $Context.GetParameterValue($licensesParameterName).Split($separator)

$Context.DirectorySearcher.AppendFilter("(sAMAccountType=805306368)")
try
{
    $searchIterator = $Context.DirectorySearcher.ExecuteSearch()
    while ($Context.MoveNext($searchIterator))
    {
        $searchResult = $searchIterator.Current
        $user = $Context.BindToObjectBySearchResult($searchResult)
        
        try
        {
            $office365Properties = $user.GetMicrosoft365Properties()
        }
        catch
        {
            continue
        }

        # Get Microsoft 365 licenses available for the user
        $licenses = $office365Properties.Licenses
        $licenseNames = New-Object System.Collections.ArrayList
        foreach ($license in $licenses)
        {
            if (!($license.Assigned))
            {
                continue
            }
            
            if ($licensesToCheck -notcontains $license.Sku.SkuPartNumber)
            {
                continue
            }
            
            $licenseNames.Add($license.Sku.DisplayName)
        }
        
        if ($licenseNames.Count -eq 0)
        {
            continue
        }
        
        # Add user to the report
        $Context.Items.Add($searchResult, @{ $licensesColumnID = ($licenseNames -join ";") }, $NULL)
    }
}
finally
{
    # Release resources
    if ($searchIterator) { $searchIterator.Dispose() }
}
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers