Check if Office 365 User is Licensed or Not using PowerShell

In this post I am going to write PowerShell script to check if a given office 365 user is licensed or not using Azure AD V2 PowerShell cmdlet Get-AzureADUser. Earlier with Old Azure AD V1 powershell command (Get-MsolUser) we had the attribute isLicensed but we don’t have the same property in latest V2 PowerShell module, so we need to use the property AssignedLicenses to check license status.

Note: Before proceed install Azure Active Directory PowerShell for Graph and run the below command to connect Azure AD V2 PowerShell module:

Connect-AzureAD

The below command checks if license is provisioned or not for the given user account:

$user = "[email protected]"
$AssignedLicenses = (Get-AzureADUser -ObjectId $user).AssignedLicenses
If ($AssignedLicenses.Count -ne 0) {
      Write-Host "Licensed"
 } Else {
        Write-Host "Not licensed"
}

Export all licensed users to CSV file

Run the below commands to export all the licensed office 365 users to csv file.

$Result=@() 
Get-AzureADUser -All $True | ForEach-Object {
if($_.AssignedLicenses.Count -ne 0){
$Result += New-Object PSObject -property @{ 
Name = $_.DisplayName
UserPrincipalName = $_.UserPrincipalName }
}}
$Result | Export-CSV "C:\LicensedO365Users.csv" -NoTypeInformation -Encoding UTF8

Export all Unlicensed users to CSV file

Run the below powershell commands to export all the office 365 users whose license is not provisioned.

$Result=@() 
Get-AzureADUser -All $True | ForEach-Object {
if($_.AssignedLicenses.Count -eq 0){
$Result += New-Object PSObject -property @{ 
Name = $_.DisplayName
UserPrincipalName = $_.UserPrincipalName }
}}
$Result | Export-CSV "C:\UnLicensedO365Users.csv" -NoTypeInformation -Encoding UTF8

Export license status of all Office 365 users

$Result=@() 
Get-AzureADUser -All $True | ForEach-Object {
$IsLicensed = ($_.AssignedLicenses.Count -ne 0)
$Result += New-Object PSObject -property @{ 
Name = $_.DisplayName
UserPrincipalName = $_.UserPrincipalName
IsLicensed = $IsLicensed  }
}
$Result | Export-CSV "C:\O365UsersLicenseStatus.csv" -NoTypeInformation -Encoding UTF8

Check license status for bulk users from CSV file

The below command checks whether license is applied or not for bulk azure ad users by importing users from CSV file and export the result to csv file.

$Result=@() 
Import-Csv 'C:\Users.csv' | ForEach-Object {
$user = $_."UserPrincipalName"
$userObj = Get-AzureADUser -ObjectId $user
$IsLicensed = ($userObj.AssignedLicenses.Count -ne 0)
$Result += New-Object PSObject -property @{ 
Name = $userObj.DisplayName
UserPrincipalName = $userObj.UserPrincipalName
IsLicensed = $IsLicensed }
}
$Result | Export-CSV "C:\LicenseStatusReport.csv" -NoTypeInformation -Encoding UTF8

You can use the app Microsoft Office 365 Manager to get clear insights into Office 365 license assignments. You can also generate different license reports such as license assignment overview, users based on applied license. Read the below post to know more details.

Microsoft Office 365 License Usage Reports ยป

Advertisement

Leave a Comment