Managing Microsoft 365 groups often requires insight into the members and their details. While the Get-MgGroupMember cmdlet retrieves the members of a specified group, it only provides their User IDs by default. To fetch personal details such as Display Name, User Principal Name (UPN), and Email, you need to pair it with the Get-MgUser cmdlet. This article explains how to combine these cmdlets effectively, with special emphasis on the necessity of nesting them to retrieve detailed member information.
The following script retrieves the members of a specified group and fetches their personal details:
# Retrieve members of a specified group
$groupMembers = Get-MgGroupMember -GroupId "1cbe8c31-589d-453a-a1e5-045f7f00c967"
# Initialize an array to store detailed user information
$userDetails = @()
# Loop through each group member and retrieve additional properties
foreach ($member in $groupMembers) {
$user = Get-MgUser -UserId $member.Id -Property "id, displayName, userPrincipalName"
$userDetails += [PSCustomObject]@{
Id = $user.Id
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
}
}
# Display the detailed user information
$userDetails | Select-Object Id, DisplayName, UserPrincipalName
Why Nesting Cmdlets is Necessary
By default, Get-MgGroupMember provides only the User IDs of group members, which are insufficient for fetching additional details such as Display Name and UPN. To retrieve these properties, the User ID must be passed to the Get-MgUser cmdlet. This pairing ensures you get complete and actionable user data.
Here's how the script works:
$groupMembers = Get-MgGroupMember -GroupId "Group-ID"
foreach ($member in $groupMembers) {
$user = Get-MgUser -UserId $member.Id -Property "id, displayName, userPrincipalName"
}
$userDetails += [PSCustomObject]@{
Id = $user.Id
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
}
Without this nesting, you won’t be able to fetch additional user properties.
-Property "id, displayName, userPrincipalName"
$groupMembers = Get-MgGroupMember -GroupId "Group-ID" -All
try {
$user = Get-MgUser -UserId $member.Id
} catch {
Write-Warning "Could not retrieve details for User ID: $($member.Id)"
}
Error | Cause | Solution |
The specified object was not found in the directory. | The Group ID or User ID is invalid or does not exist | Verify the Group ID using the Get-MgGroup cmdlet: cmdlet:
|
Insufficient privileges to complete the operation. | Missing permissions like GroupMember.Read.All or User.Read.All . |
Assign the required permissions to the account running the script |
No members found in the group. | The group is empty or the user has no direct access. | Confirm the group membership in Azure AD. |
Request throttled due to too many API calls. | Excessive requests when processing large groups | Add a delay between requests or process members in batches.
|
Pairing Get-MgGroupMember with Get-MgUser is essential for retrieving detailed information about group members in Microsoft 365. While Get-MgGroupMember provides only the User IDs, Get-MgUser enables you to fetch valuable properties such as Display Name, UPN, and Email. This method simplifies group management, aids in compliance reporting, and ensures a clear view of group memberships. Start using this approach today to enhance your administrative workflows!
© m365corner.com. All Rights Reserved. Design by HTML Codex