Export a list of customers with more than a certain number of licensed Office 365 users

Here’s a niche script that may not help anyone 🙂

This script runs through each of your Office 365 customers using delegated administration and exports a list of customers that have 5 or more users. Feel free to modify this as required.

This script uses the AzureAD PowerShell module, rather than the MSOnline module. If you don’t already have it, you can install it by opening PowerShell as an administrator and running:

Install-Module AzureAD

Script to export all Office 365 customers that have more than 5 licensed Office 365 users to a CSV:

$credential = Get-Credential
Connect-AzureAD -Credential $credential

$customers = Get-AzureADContract -All $true

foreach ($customer in $customers) {
    Connect-AzureAD -TenantId $customer.CustomerContextID -Credential $credential
    $users = Get-AzureADUser -All $true | Where-Object {$_.assignedlicenses}
    if ($users.count -gt 4) {
        Write-Host "$($customer.displayname) has 5 or more licensed users"
        $licenses = Get-AzureADSubscribedSku
        $SkuString = $licenses.SkuPartNumber -join ", "
        $hash = [ordered]@{
            CustomerName = $customer.DisplayName
            TenantId = $customer.CustomerContextId
            LicensedUserCount = $users.Count
            Licenses = $SkuString
        }
        $object = New-Object PSobject -Property $hash
        $object | Export-Csv C:\temp\Customer5orMorelicensedusers.csv -Append -NoTypeInformation
    }    
}

Was this article helpful?

Related Articles