Adding users to Microsoft 365 groups is a common administrative task—especially when managing large teams, projects, or distribution lists. Thankfully, Microsoft Graph PowerShell provides an efficient cmdlet to automate this: New-MgGroupMember.
In this guide, we’ll explore how to use the New-MgGroupMember cmdlet, walk through real-world examples, and highlight when and why this cmdlet comes in handy.
New-MgGroupMember is a Microsoft Graph PowerShell cmdlet used to add a member to a Microsoft 365 group. The member must be represented as a directory object (typically a user), and the target group is identified by its unique Group ID.
This cmdlet allows you to add:
New-MgGroupMember -GroupId <String> -DirectoryObjectId <String>
Parameters:
$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
}
}
$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
}
Make sure your CSV file is structured like this:
UserPrincipalName,GroupId
user1@domain.com,d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c
user2@domain.com,d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c
Yes, as long as the guest user already exists in Azure AD. Otherwise, invite them first using New-MgInvitation.
Absolutely. You just need the correct GroupId and a valid user DirectoryObjectId.
The cmdlet will return an error like “One or more added object references already exist”. You can ignore it or handle it in your script.
The New-MgGroupMember cmdlet is a must-have for every Microsoft 365 admin's toolbox. Whether you're managing one group or a hundred, automating group memberships can save hours of manual work and ensure your organization runs smoothly.
Pro Tip: Always double-check the GroupId and DirectoryObjectId before adding members to avoid errors or duplication.
Have you tried using New-MgGroupMember in your admin routine? Let us know how it helped simplify your tasks!
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.
© Your Site Name. All Rights Reserved. Design by HTML Codex