"This article provides a step-by-step guide to using New-MgGroupMember cmdlet in Graph PowerShell. Learn how to add single or multiple members to a group, troubleshoot common errors, and optimize bulk operations using CSV files."
The New-MgGroupMember cmdlet is part of the Microsoft Graph PowerShell module. It allows administrators to add a member to a Microsoft 365 group. This cmdlet is essential for managing group memberships in an automated and efficient manner.
Before using the New-MgGroupMember cmdlet, ensure the following prerequisites are met:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Group.ReadWrite.All"
New-MgGroupMember -GroupId <String> -DirectoryObjectId <String> [<CommonParameters>]
Parameters:
-GroupId:
The unique identifier of the Microsoft 365 group to which the member will be added.-DirectoryObjectId:
The unique identifier of the directory object (usually a user or service principal) to add to the group.<CommonParameters>:
This cmdlet supports common parameters like -Debug
, -ErrorAction
, -ErrorVariable
, -InformationAction
, -InformationVariable
, -OutVariable
, -OutBuffer
, -PipelineVariable
, and -WarningAction
. For more information see about_CommonParameters
.$groupId = "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c"
$userId = "5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f"
try {
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
Write-Host "User with ID $userId has been successfully added to the group with ID $groupId." -ForegroundColor Green
} catch {
Write-Host "Failed to add user to the group. Error: $_" -ForegroundColor Red
}
$groupId = "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c"
$userIds = @("5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f", "6d7e8f70-6e7b-41d2-a6f7-9c85d7f16e9d")
foreach ($userId in $userIds) {
try {
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
Write-Host "User with ID $userId successfully added to the group with ID $groupId." -ForegroundColor Green
} catch {
Write-Host "Failed to add user with ID $userId to the group. Error: $_" -ForegroundColor Red
}
}
To add members to a group from a CSV file, follow these steps:
Ensure your CSV file (members.csv
) contains headers like UserPrincipalName
and GroupId
. Here is an example of how your CSV file should look:
UserPrincipalName,GroupId
user1@domain.com,d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c
user2@domain.com,d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c
$csvPath = "C:\path\to\your\members.csv"
$members = Import-Csv -Path $csvPath
foreach ($member in $members) {
$user = Get-MgUser -UserPrincipalName $member.UserPrincipalName
New-MgGroupMember -GroupId $member.GroupId -DirectoryObjectId $user.Id
}
To add Group Members, select the group >> Membership tab >> Members tab >> Add Members >> select the user >> click Add button.
-ErrorAction Stop
to catch errors and handle them appropriately in your script.$groupId = "GROUP_ID"
Import-Csv "users.csv" | ForEach-Object {
$userId = $_.UserId
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
}
Description: Resource 'GroupId'
does not exist or one of its queried reference-property objects are not present.
Solution: Verify that the GroupId
and DirectoryObjectId
are correct and exist in your directory using Get-MgGroup and Get-MgUser cmdlets respectively.
Description: Attempting to add a user who is already a member of the group results in an error.
Cause: The user is already a member, and duplicate additions are not allowed.
Solution: Check group membership before attempting to add the user using Get-MgGroupMember cmdlet:
$existingMember = Get-MgGroupMember -GroupId $GroupId -UserId $UserId
if (-not $existingMember) {
New-MgGroupMember -GroupId $GroupId -UserId $UserId
} else {
Write-Output "User is already a member of the group."
}
Description: You might encounter an error if your account lacks the necessary permissions to add members to a group.
Cause: The account used does not have sufficient administrative rights.
Solution: Ensure that your account has the "Group Administrator" or similar role that allows adding members to groups. Then reconnect using Connect-MgGraph
What is New-MgGroupMember?
New-MgGroupMember is a Microsoft Graph PowerShell cmdlet that allows administrators to add users or service principals to a group in Microsoft 365. This cmdlet supports adding members to security groups and Microsoft Teams groups.
Can I use New-MgGroupMember to add guest users to a group?
Yes, you can use New-MgGroupMember to add guest users to a group. However, ensure the guest user is already added to the tenant, and you have their Object ID or UserPrincipalName for the operation.
Can I use email addresses instead of object IDs to add members?
No. The -DirectoryObjectId parameter strictly requires the Azure AD object ID of the user. You’ll need to resolve the user’s ID using a separate Get-MgUser call like:
(Get-MgUser -UserId "user@domain.com").Id
Does this cmdlet work for Microsoft 365 Groups and Security Groups?
Yes. New-MgGroupMember can be used to add members to both Microsoft 365 Groups and Azure AD Security Groups. However, for Microsoft Teams, make sure the group is team-enabled, or use Add-MgTeamMember for role-specific membership (like owner/member).
How to verify group membership after adding users to the group?
Run Get-MgGroupMember -GroupId $groupId command. This should list the User Id of the user(s) you just added to the group.
@odata.id
Must Follow Microsoft Graph FormatNew-MgGroupMemberByRef
, you must pass the object reference in the correct Microsoft Graph format:https://graph.microsoft.com/v1.0/directoryObjects/{ObjectId}
Passing just the Object ID or UPN will result in errors. This format ensures the API can properly locate and bind the member object.
New-MgGroupMemberByRef
, you can add:
Get-MgUser
Get-MgServicePrincipal
Get-MgDevice
@odata.id
reference.
The New-MgGroupMember cmdlet is a powerful tool for managing group memberships in Microsoft 365. By understanding its syntax, usage, and potential pitfalls, administrators can efficiently manage group memberships and automate related tasks. Whether you're handling day-to-day operations or implementing complex onboarding workflows, this cmdlet provides the functionality needed to keep your groups up-to-date and secure.
For more detailed information, refer to the official Microsoft documentation for the New-MgGroupMember cmdlet.
20 Graph PowerShell cmdlets with easily accessible "working" examples.
Example:
© m365corner.com. All Rights Reserved. Design by HTML Codex