Script Repository


Microsoft 365 licenses assigned to users

September 29, 2021
140

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.GetOffice365Properties()
        }
        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 )
No results found.
Leave a comment