Migrating from Get-AzureADDirectoryRoleMember to Get-MgDirectoryRoleMember

As Microsoft moves away from the legacy AzureAD module, it’s essential to migrate your administrative scripts to the Microsoft Graph PowerShell SDK. One such shift involves replacing Get-AzureADDirectoryRoleMember with its Graph-based equivalent: Get-MgDirectoryRoleMember.

This article explains the migration and provides real-world examples to help you make the transition with ease.


What You Did Previously with Get-AzureADDirectoryRoleMember?

With the AzureAD module, you may have used commands like:

Get-AzureADDirectoryRoleMember -ObjectId 

These commands allowed you to retrieve directory roles, look them up by name, and list members in each role.

This helped you retrieve users, service principals, or groups assigned to a specific directory role. For more detailed information, additional commands like Get-AzureADUser were often used.


What You Should Do Now with Get-MgDirectoryRoleMember

With the Graph PowerShell SDK, the equivalent is:

Get-MgDirectoryRoleMember -DirectoryRoleId <DirectoryRoleId>

You can also enhance the output by combining it with Get-MgUser and using features like -ExpandProperty to access nested data (e.g., managers).

Example 1: Retrieve All Members of a Directory Role

$roleId = "f8cdef31-a31e-4b4a-93e4-5f571e91255a" # Replace with your DirectoryRoleId
Get-MgDirectoryRoleMember -DirectoryRoleId $roleId

This fetches all members (users, service principals, groups) assigned to a specific directory role.

Example 2: Retrieve Directory Role Members Info

$roleId = "f8cdef31-a31e-4b4a-93e4-5f571e91255a"
$members = Get-MgDirectoryRoleMember -DirectoryRoleId $roleId

foreach ($member in $members) {
    Get-MgUser -UserId $member.Id | Select-Object Id, DisplayName, UserPrincipalName
}

Use Get-MgUser cmdlet with Get-MgDirectoryRoleMember to extract detailed user info

Example 3: Filtering Based on Specific Properties of Directory Role Members

$roleId = "f8cdef31-a31e-4b4a-93e4-5f571e91255a"
$members = Get-MgDirectoryRoleMember -DirectoryRoleId $roleId

$filteredMembers = foreach ($member in $members) {
    $user = Get-MgUser -UserId $member.Id
    if ($user.DisplayName -like "Admin*") {
        $user
    }
}

$filteredMembers | Select-Object Id, DisplayName, UserPrincipalName

Filter users based on specific attributes like DisplayName, Department, or JobTitle.

Example 4: Retrieve Members with Manager Details Using -ExpandProperty

$roleId = "f8cdef31-a31e-4b4a-93e4-5f571e91255a"
$members = Get-MgDirectoryRoleMember -DirectoryRoleId $roleId

foreach ($member in $members) {
    $userWithManager = Get-MgUser -UserId $member.Id -ExpandProperty "manager"
    if ($userWithManager.Manager) {
        $managerDisplayName = $userWithManager.Manager.AdditionalProperties["displayName"]
        [PSCustomObject]@{
            UserId             = $userWithManager.Id
            DisplayName        = $userWithManager.DisplayName
            UserPrincipalName  = $userWithManager.UserPrincipalName
            ManagerDisplayName = $managerDisplayName
        }
    }
}

This example fetches both the role member and their manager's name using Microsoft Graph's expand feature.


What’s Different with Get-MgDirectoryRoleMember?


Feature Get-AzureADDirectoryRoleMember Get-MgDirectoryRoleMember
Module AzureAD Microsoft.Graph
Object Types Returned User, Group, ServicePrincipal Same
Output Detail Moderate (includes displayName, UPN) Minimal (ID only)
Manager Expansion Not supported ✅ Supported via -ExpandProperty in Get-MgUser
Future-proof ❌ Deprecated ✅ Supported and actively developed

Conclusion

Migrating from Get-AzureADDirectoryRoleMember to Get-MgDirectoryRoleMember is a straightforward but important step in adopting the Microsoft Graph PowerShell SDK. While the newer cmdlet offers a similar core function, it gives you more flexibility and deeper integration capabilities, especially when combined with Get-MgUser.

Use the examples above to confidently upgrade your scripts and future-proof your administrative automation.

Visit M365Corner.com for ready-to-use free Microsoft Graph PowerShell tools and step-by-step migration guides built for Microsoft 365 administrators.


Permission Required

Example:


                                


                                


                                

© Your Site Name. All Rights Reserved. Design by HTML Codex