Managing group memberships in Microsoft 365 is essential for administrators. The Get-MgGroupMember cmdlet allows you to retrieve group member details, but it only provides limited information, such as user IDs. To fetch additional details like Display Name and User Principal Name (UPN), you can pair it with the Get-MgUser cmdlet. This article demonstrates how to combine these two cmdlets to retrieve and display detailed user information for group members.
# Retrieve members of a specified group
$groupId = "1cbe8c31-589d-453a-a1e5-045f7f00c967" # Replace with your group's ID
$groupMembers = Get-MgGroupMember -GroupId $groupId
# 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
Script Output:
$groupMembers = Get-MgGroupMember -GroupId $groupId
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
}
$userDetails | Select-Object Id, DisplayName, UserPrincipalName
Error | Cause | Solution |
---|---|---|
Insufficient privileges to complete the operation. | Missing permissions like Group.Read.All or User.Read.All. | Grant the necessary permissions in Azure AD or use a Global Admin account. |
The specified object was not found in the directory. | Invalid group ID or user ID. | Double-check the group ID and ensure it exists in your tenant. |
The user does not have access to perform the operation. | Lack of access to group or user details. | Ensure proper role assignments for the account running the script. |
Combining Get-MgGroupMember and Get-MgUser is a powerful way to retrieve detailed group membership information in Microsoft 365. This script simplifies the process of mapping user details to group memberships, making it easier for administrators to audit, troubleshoot, and document their environments. Try this today to streamline your Microsoft 365 management tasks!
© m365corner.com. All Rights Reserved. Design by HTML Codex