Script Repository

Active users without Microsoft 365 licenses

April 28, 2020

The script sends a report containing active user accounts that do not have Microsoft 365 (Office 365) licenses assigned. To run the script, create a Scheduled Task configured for the Domain-DNS object type.

To use the script, install the Microsoft Azure Active Directory PowerShell Module on the computer where Adaxes service runs.


  • $to - Specifies the recipient email address.
  • $subject - Specifies the email notification subject.
  • $reportHeader - Specifies the report header.
  • $reportFooter - Specifies the report footer.
Edit Remove
$to = "" # TODO: modify me
$subject = "Active users without Microsoft 365 licenses assigned" # TODO: modify me
$reportHeader = "<h2>Active users without Microsoft 365 licenses assigned</h2><br/>" # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

$scriptBlock = {
    Import-Module MsOnline
    Connect-MsolService -Credential $microsoft365Cred
    [System.Object[]]$unlicensedUsersOnly = Get-MsolUser -All -UnlicensedUsersOnly
    return ,$unlicensedUsersOnly

    $unlicensedUsersOnly = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $Context.GetOffice365Credential() -ErrorAction Stop
    $Context.LogMessage("An error occurred when searching active users without Microsoft 365 licenses. Error: " + $_.Exception.Message, "Warning")
    $unlicensedUsersOnly = $NULL

# Add users to report
$report = New-Object "System.Text.StringBuilder"

if ($unlicensedUsersOnly -eq $NULL)
    $report.Append("<b>No users found</b>")

if ($unlicensedUsersOnly -ne $NULL)
    foreach ($user in $unlicensedUsersOnly)

# Send mail
$Context.SendMail($to, $subject, $NULL, $report.ToString())

Comments ( 0 )
No results found.
Leave a comment