"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
(Get-MgUser -UserId "user@domain.com").Id
New-MgGroupMember
, the membership may not reflect immediately due to backend processing.Get-MgGroupMember
.
@odata.id
Must Follow Microsoft Graph Formathttps://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.
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.
Note: To add members to a Microsoft 365 or security group using Graph API, send a POST
request to /groups/{group-id}/members/$ref
with the proper user reference (@odata.id)
pointing to the user’s object URL.
Add a Single Member to a Group
# Replace with actual groupId and userId (GUIDs)
$groupId = "b320ca29-3775-4bb7-a0f8-e9abee3cdb2c"
$userId = "2e539763-2706-464d-ba5a-8e94f1c552d5"
# Define the user reference payload
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$userId"
}
# Add the user to the group
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/groups/$groupId/members/`$ref" -Body ($body | ConvertTo-Json)
Bulk Add Members to a Group from CSV
# Sample CSV headers: userId
$groupId = "b320ca29-3775-4bb7-a0f8-e9abee3cdb2c"
$csvPath = "C:\Users\admin\Documents\group-members.csv"
$users = Import-Csv -Path $csvPath
foreach ($user in $users) {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$($user.userId)"
}
$uri = "https://graph.microsoft.com/v1.0/groups/$groupId/members/`$ref"
Invoke-MgGraphRequest -Method POST -Uri $uri -Body ($body | ConvertTo-Json)
}
CSV Format Example
userId
2e539763-2706-464d-ba5a-8e94f1c552d5
9a703db1-9ff8-4d60-bd64-4cceac69ad42
e0433c58-1d2f-4e5c-82cd-a111aa551c20
💡 The userId refers to the Azure AD Object ID of the user. Use Get-MgUser | Select Id, DisplayName
to fetch them.
Required Permissions
To add members to groups using Graph API, you need:
GroupMember.ReadWrite.All
Directory.ReadWrite.All
Graph API Documentation
👉 POST /groups/{id}/members/$ref - Microsoft Graph v1.020 Graph PowerShell cmdlets with easily accessible "working" examples.
Example:
© m365corner.com. All Rights Reserved. Design by HTML Codex