As Microsoft gradually phases out the AzureAD module in favor of the Microsoft Graph PowerShell SDK, many administrators are transitioning from Get-AzureADDirectoryRole to Get-MgDirectoryRole.
This migration not only ensures continued compatibility with Microsoft 365 but also offers access to a broader range of APIs via a single unified platform — Microsoft Graph.
In this article, we'll walk through how to migrate your scripts and understand the differences using real-world examples.
With the AzureAD module, administrators commonly used:
Get-AzureADDirectoryRole
Get-AzureADDirectoryRole | Where-Object { $_.DisplayName -eq "Global Administrator" }
Get-AzureADDirectoryRoleMember -ObjectId <roleObjectId>
These commands allowed you to retrieve directory roles, look them up by name, and list members in each role.
The Graph PowerShell replacement for these commands is:
Get-MgDirectoryRole
Or with selected properties:
Get-MgDirectoryRole | Select ID, DisplayName, Description
This command retrieves all activated directory roles in your tenant.
Get-MgDirectoryRole -DirectoryRoleId | Select ID, DisplayName, Description
Use this to fetch a specific directory role by its unique ID (GUID).
Get-MgDirectoryRole | Where-Object { $_.DisplayName -eq "Global Administrator" } | Select *
Since Graph cmdlets don’t directly support filtering by DisplayName, we pull all roles and filter in PowerShell.
$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 loop lists all activated roles and their respective members — very useful for access audits.
Feature | Get-AzureADDirectoryRole | Get-MgDirectoryRole |
Module | AzureAD | Microsoft.Graph |
Filter Support | Basic filtering | Some filters limited; use client-side filters |
Consistency | Azure AD-specific | Unified Graph platform |
Availability | Deprecated | Actively supported and maintained |
Expansion | Limited | Supports -ExpandProperty, -Property, and richer output |
🔔 Important: Only activated roles (i.e., those with members) appear in the results of Get-MgDirectoryRole.
Migrating from Get-AzureADDirectoryRole to Get-MgDirectoryRole is a necessary and future-proof step as the AzureAD module becomes deprecated. While the syntax is similar, the Microsoft Graph SDK offers greater flexibility, richer integrations, and a unified programming model.
With the examples above, you can start converting your legacy scripts confidently — ensuring compatibility, automation, and scalability with Microsoft 365.
Visit M365Corner.com for ready-to-use free Microsoft Graph PowerShell tools and step-by-step migration guides built for Microsoft 365 administrators.
© Your Site Name. All Rights Reserved. Design by HTML Codex