How to find the license count of O365 (Currently Microsoft 365) users

O365 which is recently renamed as Microsoft 365, however it has been used in most of the corporates for a long time now. To know the users license count is one of the important task which is very common for most of the O365 admins. Here is a script written by me which can give you the list of the users who has been assigned this license. In this script I am finding the list of ATP_ENTERPRISE license details. Based on your requirement you can change the parameters.


$userslist = Get-MsolUser -All | ?{$_.isLicensed -eq "TRUE" -and ($_.Licenses | ?{ $_.AccountSkuId -like "*corp:ATP_ENTERPRISE*"})}
$Result = @()

foreach ($user in $userslist) {

$UserName = $null
$UserUPN = $null
$Userliceses= $null
$liceseresult = $null

$UserName = $user.DisplayName
$UserUPN = $user.UserPrincipalName

$Templicense = $user| Select-Object -ExpandProperty Licenses | select -ExpandProperty AccountSkuId

foreach ($license in $Templicense) {
$Lic = $("$license;")
$liceseresult += $Lic
}

$Prop =@{
'UserName' = $UserName;
'UPN' = $UserUPN
'AssignedLicenses' = $liceseresult
}
$Obj = New-Object -TypeName psobject -Property $Prop
$Result += $Obj
$Result | select UserName,UPN,AssignedLicenses | Export-Csv -Path "C:\Users\domainid\Desktop\O365\LicesnsesResult.csv" -NoTypeInformation