Microsoft 365 offers several directory roles that define what users can and cannot do in your tenant. Whether you're managing Global Administrators, Exchange Admins, or custom roles, it's essential to have visibility into these assignments. Fortunately, the Get-MgDirectoryRole cmdlet in Microsoft Graph PowerShell makes this process easy.
In this article, we'll explore what the cmdlet does, why it's useful, and how to use it with practical examples.
The Get-MgDirectoryRole cmdlet retrieves the directory roles that are currently activated in your Microsoft 365 tenant. This includes built-in roles like Global Administrator, User Administrator, Teams Administrator, and more.
Note: Only activated roles will appear—roles are "activated" when at least one user is assigned to them.
You may want to use Get-MgDirectoryRole for several reasons:
Combined with Get-MgDirectoryRoleMember, this cmdlet forms the foundation for Microsoft 365 role audits.
Get-MgDirectoryRole -DirectoryRoleId <directoryRoleId>
Parameters:
Get-MgDirectoryRole | Select ID, DisplayName, Description
This command retrieves all active directory roles in your tenant along with their ID and description.
Get-MgDirectoryRole -DirectoryRoleId | Select ID, DisplayName, Description
Use this when you have the role ID and want to fetch details about that specific role.
Get-MgDirectoryRole | Where-Object { $_.DisplayName -eq "Global Administrator" } | Select *
Searches for a directory role with the exact display name, such as "Global Administrator".
To list users who are assigned to directory roles, use Get-MgDirectoryRoleMember alongside Get-MgDirectoryRole:
$roles = Get-MgDirectoryRole
foreach ($role in $roles) {
Write-Host "`nRole: $($role.DisplayName)"
$members = Get-MgDirectoryRoleMember -DirectoryRoleId $role.Id
foreach ($member in $members) {
Write-Output " - $($member.AdditionalProperties.displayName) ($($member.AdditionalProperties.userPrincipalName))"
}
}
This script loops through each directory role and displays the display name and UPN of every user assigned to it.
💡 Note: The AdditionalProperties property is where user details like displayName and userPrincipalName are stored.
Only active roles (those with assigned members) appear with Get-MgDirectoryRole.
Use Get-MgDirectoryRoleTemplate to retrieve a complete list of available role templates.
Use New-MgDirectoryRoleMemberByRef with the directory role ID and the user's object ID.
The Get-MgDirectoryRole cmdlet is your go-to tool for fetching activated directory roles in Microsoft 365. When paired with Get-MgDirectoryRoleMember, you gain full visibility into who has administrative access and can make informed decisions about role-based access control.
Whether you're auditing, securing, or reporting, these tools are essential for any Microsoft 365 administrator.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex