Managing group memberships is one of the essential tasks for Microsoft 365 administrators. Groups are the backbone of collaboration in Microsoft 365, giving users access to shared resources like Teams, SharePoint sites, and mailboxes. Adding members to groups efficiently is critical to maintaining productivity and access control.
The New-MgGroupMember cmdlet, part of the Microsoft Graph PowerShell module, provides a streamlined way to add members to Microsoft 365 groups programmatically. In this guide, we’ll explore everything you need to know to get started, along with practical examples and advanced tips.
A Microsoft 365 group member is a user or service account that belongs to a specific group. These members are granted access to resources associated with the group, including:
Group members can have different roles:
The New-MgGroupMember cmdlet simplifies adding users to Microsoft 365 groups, providing key benefits like:
To use the New-MgGroupMember cmdlet, you need to set up the Microsoft Graph PowerShell module.
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph
Disconnect-MgGraph
Here are some common use cases for adding members to groups using New-MgGroupMember:
To add a single user to a group, use:
$groupId = "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c"
$userId = "5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f"
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
This command adds the user with the specified userId to the group identified by groupId.
If you need to add multiple users to a group, iterate through an array of user IDs:
$groupId = "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c"
$userIds = @("5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f", "6d7e8f70-6e7b-41d2-a6f7-9c85d7f16e9d")
foreach ($userId in $userIds) {
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
}
This script loops through the user IDs and adds each one to the specified group.
For large-scale membership updates, import user details from a CSV file.
CSV File Example:
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
}
This script reads the user details from the CSV file, retrieves their IDs using Get-MgUser, and adds them to the appropriate group.
$existingMembers = Get-MgGroupMember -GroupId $groupId | Select-Object Id
if ($userId -notin $existingMembers.Id) {
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
}
try {
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
Add-Content -Path "C:\Logs\GroupMembershipLog.txt" -Value "Added $userId to $groupId successfully."
} catch {
Add-Content -Path "C:\Logs\GroupMembershipLog.txt" -Value "Failed to add $userId to $groupId: $_"
}
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId -WhatIf
The New-MgGroupMember cmdlet is an indispensable tool for Microsoft 365 administrators, enabling efficient and automated group membership management. Whether adding individual users, updating multiple memberships, or handling bulk updates from CSV files, this cmdlet simplifies the process and ensures accuracy.
By mastering New-MgGroupMember and incorporating best practices, you can streamline group management workflows and enhance collaboration across your organization.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© M365Corner. All Rights Reserved.