Below is a ready-to-use script. Review and adapt variables to your environment.
# Requires: AzureAD or Microsoft Graph
Connect-MgGraph -Scopes 'User.Read.All','Organization.Read.All' -NoWelcome
$users = Get-MgUser -All -Property Id,DisplayName,UserPrincipalName,AssignedLicenses
$report = foreach ($u in $users) {
[PSCustomObject]@{ User=$u.DisplayName; UPN=$u.UserPrincipalName; Licenses=($u.AssignedLicenses.Count) }
}
$report | Export-Csv -NoTypeInformation -Path licenses.csv