Get user's license type based on new Azure AD Group based licensing using PowerShell

Featured image

Recently Microsoft released much awaited group based license management in Azure AD for users. Now users will have direct license assigned to them or inherited group based license. In this blog, using PowerShell we shall check whether the user is assigned with direct license or group based license. In the next blog, we shall check [how to get the licenses applied to groups in Azure


Find users license type using PowerShell

The below PowerShell script will get all the users with their license type. If users are member of multiple groups with licenses applied, you will get group’s name with semi-colon separated.

Prerequisites Before starting the process, download and install Azure AD PowerShell module from this

link. Then execute the script in PowerShell (with Run as Administrator privilege) by connecting to MsolService as global admin.

Copy the below script to notepad and save it as all-users-with-license-type.ps1

``` {.black .top-margin .bottom-margin} function Users-LicenseType { Param( [System.Management.Automation.PSCredential]$cred ) Connect-MsolService -Credential $cred $Gplist= @{} $Group =Get-msolgroup # Get all groupname with group objectId foreach($gp in $Group) { $Gplist+=@{$gp.ObjectId.ToString() = $gp.DisplayName} } $users= Get-MsolUser -All $AllUser = @() # Find Users License Type foreach($user in $users) { $UserList = “” | Select “UserPrincipalName”,”LicenseType”
$lic=$user.Licenses.GroupsAssigningLicense.Guid if($lic -ne $null) { $GpName = ‘’ foreach($lc in $lic) { If($GpName) { if($Gplist.Item($lc.ToString()) -ne $null) { $GpName=$GpName + “;” + $Gplist.Item($lc.ToString()) } } Else { if($Gplist.Item($lc.ToString()) -ne $null) { $GpName=$Gplist.Item($lc.ToString()) } }
} $UserList.UserPrincipalName = $user.UserPrincipalName $UserList.LicenseType = “Inherited(“+$GpName+”)” $AllUser+= $UserList $UserList =$null

    $UserList.UserPrincipalName = $user.UserPrincipalName
    $UserList.LicenseType = "Direct"
    $AllUser+= $UserList
    $UserList =$null
return $AllUser }

$cred =Get-Credential

$Listofusers = Users-LicenseType -cred $cred


#### Example 1: Get all user's license license type {.h4 .bold}

``` {.black .top-margin .col-md-6}
PS C:\PowershellQuery> & '.\all-users-with-license-type.ps1    


Example 2: Export all users license type to csv

``` {.black .top-margin .col-md-7} PS C:\PowershellQuery> & ‘.\all-users-with-license-type.ps1’ |Export-Csv “C:\PowershellQuery\ExportAllUser.csv” -NoTypeInformation

#### Example 3:

**How to find license type for an user**

``` {.black .top-margin}
PS C:\PowershellQuery> & '.\all-users-with-license-type.ps1' | Where-Object {$_.UserPrincipalName -like  ""}


Find license type for multiple users

{.black .top-margin} PS C:\PowershellQuery> & '.\all-users-with-license-type.ps1' | Where-Object {$_.UserPrincipalName -like "" -or $_.UserPrincipalName -like ""}