New-MgGroupMember

What is New-MgGroupMember?

The New-MgGroupMember cmdlet in Microsoft Graph PowerShell is used to add members to Microsoft 365 groups. It supports adding single users, multiple users, or even bulk members via a CSV file.


Why Use New-MgGroupMember?

Group membership management is central to collaboration and security. With New-MgGroupMember, administrators can:

  • Automate adding users to security or Office 365 groups.
  • Bulk assign group memberships during onboarding.
  • Ensure consistency across multiple groups.
  • Save time compared to manually assigning memberships in the admin center.

Prerequisites

Before using this cmdlet, connect to Microsoft Graph with the required permissions:

Connect-MgGraph -Scopes "Group.ReadWrite.All"

How to Use New-MgGroupMember?

Syntax (essential parameters):

New-MgGroupMember -GroupId <String> -DirectoryObjectId <String> 

The cmdlet requires the group ID and the object ID of the user (or directory object) you want to add.


New-MgGroupMember Examples

  • Example 1: Add a User to a Group
  • $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
    }
                                                
  • Example 2: Add Multiple Users to a Group
  • $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
    }
    }
                                                
  • Example 3: Add Members from a CSV File
  • CSV File (members.csv)

    UserPrincipalName,GroupId
    user1@domain.com,d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c
    user2@domain.com,d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0
    user3@domain.com,cd9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c

    PowerShell Script

    $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
    }
                                                

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