This guide provides an in-depth look at the New-MgGroupMemberByRef cmdlet in Microsoft Graph PowerShell. Learn how to efficiently add members to Microsoft 365 groups using the reference-based approach, ideal for bulk operations and advanced scenarios.
The New-MgGroupMemberByRef cmdlet is a powerful tool within the Microsoft Graph PowerShell module that allows administrators to add members to a Microsoft 365 group by referencing their directory objects. This cmdlet is particularly useful for bulk additions, streamlining group management tasks, and integrating with automated scripts.
New-MgGroupMemberByRef -GroupId <String> -BodyParameter <Hashtable>
Parameters:
-GroupId: The unique identifier (ID) of the group where the members will be added.
-BodyParameter: A hashtable containing the necessary properties to reference the users being added.
1. Adding a Single Member to a Group:
To add a single user to a group, you can use the following example:
$NewMember = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/12345678-90ab-cdef-1234-567890abcdef"
}
New-MgGroupMemberByRef -GroupId "87654321-fedc-ba98-7654-3210fedcba98" -BodyParameter $NewMember
This example adds a user with the ID 12345678-90ab-cdef-1234-567890abcdef to the group with the ID 87654321-fedc-ba98-7654-3210fedcba98.
2. Adding Multiple Members to a Group:
For adding multiple members, you can create a list of users and iterate through them:
$Members = @(
@{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/12345678-90ab-cdef-1234-567890abcdef"
}
@{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/23456789-0abc-def1-2345-67890abcdef1"
}
)
foreach ($Member in $Members) {
New-MgGroupMemberByRef -GroupId "87654321-fedc-ba98-7654-3210fedcba98" -BodyParameter $Member
}
This script adds two users to the specified group.
3. Importing Members from a CSV File:
For large-scale operations, importing members from a CSV file can be highly efficient. Here’s how:
CSV Format:
UserId
12345678-90ab-cdef-1234-567890abcdef
23456789-0abc-def1-2345-67890abcdef1
PowerShell Script:
$GroupId = "87654321-fedc-ba98-7654-3210fedcba98"
$CsvPath = "C:\Path\To\Users.csv"
$Users = Import-Csv -Path $CsvPath
foreach ($User in $Users) {
$NewMember = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$($User.UserId)"
}
New-MgGroupMemberByRef -GroupId $GroupId -BodyParameter $NewMember
}
This script imports user IDs from a CSV file and adds them to the specified group.
Checking for the Newly Added Group Members
Execute Get-MgGroupMember and provide the Group-Id. The newly added users will get listed.
Differences Between New-MgGroupMemberByRef and New-MgGroupMember Cmdlets
Error | Cause | Fix |
"Request_BadRequest" Error | The -BodyParameter hashtable is not correctly formatted. | Double-check that the @odata.id property is properly formatted and includes the full URL as required. |
"ResourceNotFound" Error | The user or group ID provided does not exist. | Verify that the IDs are correct and that the users and groups exist in your directory. |
"Authorization_RequestDenied" Error | Insufficient permissions to add members to the group. | Ensure that your account has the necessary permissions (e.g., Group Administrator) to perform this action. Group.ReadWrite.All is the required Graph API permission. |
GroupId,MemberReference
<YourGroupId1>,https://graph.microsoft.com/v1.0/directoryObjects/<ObjectId1>
<YourGroupId2>,https://graph.microsoft.com/v1.0/directoryObjects/<ObjectId2>
Here’s the PowerShell script to process the CSV and add members to their respective groups:
$Data = Import-Csv -Path "C:\Path\To\File.csv"
foreach ($Row in $Data) {
$Body = @{
"@odata.id" = $Row.MemberReference
}
New-MgGroupMemberByRef -GroupId $Row.GroupId -BodyParameter $Body
}
@odata.id
FormatNew-MgGroupMemberByRef
, the member must be passed in the correct format:"https://graph.microsoft.com/v1.0/directoryObjects/{ObjectId}"
Supplying just the Object ID or UPN will result in an error. Always wrap it in the full @odata.id
path.
New-MgGroupMemberByRef
cmdlet can add a variety of directory object types to a group, including:
Get-MgUser
Get-MgDevice
Get-MgServicePrincipal
New-MgGroupMemberByRef
.The New-MgGroupMemberByRef cmdlet is a versatile tool for managing group memberships in Microsoft 365. Whether adding single users, multiple users, or importing users from a CSV file, this cmdlet simplifies the process and can be easily integrated into your automation scripts. By understanding the differences between New-MgGroupMemberByRef and New-MgGroupMember, you can choose the most appropriate cmdlet for your needs, ensuring efficient and effective group management in your organization.
© m365corner.com. All Rights Reserved. Design by HTML Codex