Privileged accounts in Microsoft Entra are user accounts with a high level of permission that if compromised, could severely impact business operations. There are many good practices for securing privileged accounts, from utilising just-in-time access with Privileged Identity Management to creating strong Conditional Access policies, such as location-based or session controls. However, an often overlooked cause for concern is privileged accounts with excessive Microsoft 365 license assignments.
In this article, we will explore why you shouldn’t over-license your privileged accounts and how you can identify them in your environment using Microsoft Graph PowerShell.
Why privileged accounts shouldn't be assigned Microsoft 365 licenses
When privileged accounts have access to applications such as Exchange and Teams, which are automatically activated when assigning a supporting Microsoft 365 license, they are exposed to social engineering attacks, causing an increased risk of compromise. Reducing the application footprint of your privileged accounts removes any unnecessary attack vectors and thus reduces the risk.
A properly licensed privileged account in Microsoft 365 should:
- Be assigned a Microsoft Entra P1 or P2 license.
- Not be licensed.
By ensuring your privileged accounts fall into either of the above two categories, you work safely knowing you are operating with a reduced attack footprint. If you want to understand more about why in some scenarios you don’t need to license some accounts, start with this article: Understanding Microsoft Entra Licensing With Multiple Tenants. While it details Entra entitlements across multiple organisation-owned tenants, the same concept applies when a user has two accounts in the same tenant.
Requirements
To run the scripts in this article you will need the Microsoft Graph PowerShell SDK installed and access to a Global Administrator account to consent to the requirement permissions.
Finding over licensed privileged accounts
When searching for members of Microsoft Entra roles, it is important to consider using Privileged Identity Management for organisations licensed for Microsoft Entra P2. If you were to view the members of directory roles in organisations using Entra Basic or Entra P1, then you would probably get an accurate representation of role members. However, for organisations using Privileged Identity Management, who have made users eligible to ‘PIM’ into their role, these users will not be present as a directory role member until after they elevate their role membership. In this scenario, if you were to view all directory role members while users are not active in their roles, then you would not get an accurate representation of role members.
To solve this problem, you need to first check which Entra license plan you have activated in your tenant, then compare both active and eligible role members in your directory.
Start by connecting to Microsoft Graph PowerShell with the necessary permissions to view directory objects and directory roles.
Connect-mggraph -scope Organization.Read.All,RoleManagement.Read.Directory,User.Read.All Once connected, you need to apply some logic to determine if your tenant is activated for Entra P2, P1, or Basic. The script below will search for the activated SKUs, first searching for Entra P2 and breaking the loop if found, then moving on to P1. The result is then saved into the $licenseplan variable.
$items = @("AAD_PREMIUM_P2", "AAD_PREMIUM", "AAD_BASIC")
$Skus = Get-MgSubscribedSku
foreach ($item in $items) {
$Search = $skus | ? {$_.ServicePlans.servicePlanName -contains "$item"}
if ($Search) {
$licenseplan = $item
break
} ElseIf ((!$Search) -and ($item -eq "AAD_BASIC")){
$licenseplan = $item
break
}
} The next step is to generate a list of unique users assigned to privileged roles. Here, I used an IF statement to get all users assigned or eligible to privileged roles through PIM if the license plan is Entra P2, or to get all members assigned directly to the directory roles if the license plan is anything else. The below script will find only User objects assigned to roles and will exclude Service Principals or Groups.
If ($licenseplan -eq "AAD_PREMIUM_P2") {
$EligiblePIMRoles = Get-MgRoleManagementDirectoryRoleEligibilitySchedule -All -ExpandProperty *
$AssignedPIMRoles = Get-MgRoleManagementDirectoryRoleAssignmentSchedule -All -ExpandProperty *
$DirectoryRoles = $EligiblePIMRoles + $AssignedPIMRoles
$PrivilegedRoles = $DirectoryRoles | Where-Object {
($_.RoleDefinition.DisplayName -like "*Administrator*") -or ($_.RoleDefinition.DisplayName -like "*Writer*") -or ($_.RoleDefinition.DisplayName -eq "Global Reader")
}
$PrivilegedRoleUsers = $PrivilegedRoles | Where {$_.Principal.AdditionalProperties.'@odata.type' -eq "#microsoft.graph.user"}
$RoleMembers = $PrivilegedRoleUsers.Principal.AdditionalProperties.userPrincipalName | Select-Object -Unique
$PrivilegedUsers = $RoleMembers | ForEach-Object { Invoke-MgGraphRequest -uri "/beta/users/$($_)?`$select=displayName,UserPrincipalName,AssignedLicenses" -OutputType PSobject }
}else{
$DirectoryRoles = Invoke-MgGraphRequest -Uri "/beta/directoryRoles?" -OutputType PSObject | Select -Expand Value
$PrivilegedRoles = $DirectoryRoles | Where-Object {
($_.DisplayName -like "*Administrator*") -or ($_.DisplayName -like "*Writer*") -or ($_.DisplayName -eq "Global Reader")
}
$RoleMembers = $PrivilegedRoles | ForEach-Object { Invoke-MgGraphRequest -uri "/beta/directoryRoles/$($_.id)/members" -OutputType PSObject | Select -Expand Value} | Select-Object Id -Unique
$PrivilegedUsers = $RoleMembers | ForEach-Object { Invoke-MgGraphRequest -uri "/beta/users/$($_.id)?`$select=displayName,UserPrincipalName,AssignedLicenses" -OutputType PSobject }
} Finally, the last step is to analyse which licenses are assigned to the privileged users. With this, we are checking if a privileged account is assigned anything other than the Entra Premium license SKUs and if so, it is added to the over-licensed privileged user’s list and output to a grid window for review.
#Generate report
$overLicensedPrivUsers = [System.Collections.Generic.List[Object]]::new()
$translationTable = Invoke-RestMethod -Method Get -Uri "https://download.microsoft.com/download/e/3/e/e3e9faf2-f28b-490a-9ada-c6089a1fc5b0/Product%20names%20and%20service%20plan%20identifiers%20for%20licensing.csv" | ConvertFrom-Csv
Foreach ($user in $PrivilegedUsers){
$licenses = @()
If (($user.assignedLicenses.skuid -notin "41781fb2-bc02-4b7c-bd55-b576c07bb09d,eec0eb4f-6444-4f95-aba0-50c24d67f998") -and ($user.assignedLicenses.skuid.count -gt 0)){
foreach($guid in $user.assignedLicenses.skuid) {
$temp = ($translationTable | Where-Object {$_.GUID -eq $guid} | Sort-Object Product_Display_Name -Unique).Product_Display_Name
$licenses += $temp
}
$Obj2 = [pscustomobject][ordered]@{
DisplayName = $user.DisplayName
UserPrincipalName = $User.UserPrincipalName
License = $licenses -join [environment]::NewLine
}
$overLicensedPrivUsers.Add($Obj2)
}
}
$overLicensedPrivUsers | Out-GridView Just the first step...
Ensuring your privileged accounts are correctly licensed is just a small part to play in successfully cost-optimising and securing your Microsoft 365 tenant. So don’t stop here, follow for my next article on how to correctly license-optimise your Microsoft 365 tenant.

