Add User to Multiple Microsoft 365 Groups Using Graph PowerShell

Let's say you want to assign a user to multiple groups. Example: your organization sales guys need to be part of multiple M365 groups to complete their work. A simple Graph PowerShell script that automates this process - that of adding the M365 user to the required groups - would be hugely helpful, as it would reduce the need of MIcrosoft 365 administrators (like you) having to manually add the user to each of these groups.

In this article, let's understand how you can create such a script that adds your Microsoft 365 user to multiple Microsoft 365 groups using Graph PowerShell.

Install the Microsoft Graph PowerShell SDK

If you haven't already, install the Graph PowerShell module using the following PowerShell command. PowerShell command to install Microsoft Graph module

Connect to Microsoft Graph PowerShell module with necessary permissions

Before you can interact with Microsoft Graph, you need to authenticate yourself with the required permissions. The required permissions in our case are Group.ReadWrite.All and User.ReadWrite.All

PowerShell command to connect to Microsoft Graph with required permissions

Running the Connect-MgGraph cmdlet with the required permissions opens will open a login prompt for you to enter your Microsoft 365 credentials and authenticate yourself.

Use Get-MgGroup cmdlet to get required Group IDs

Run Get-MgGroup command to get the IDs of the Microsoft 365 Groups you wish to add the user to.

Graph PowerShell command to retrieve Microsoft 365 group IDs

Using Get-MgUser to get the UserPrincipalName or User ID

Run Get-MgUser command and get the UserPrincipalName of the user who is going to be added to the Microsoft 365 Groups.

Graph PowerShell command to retrieve user principal name or user id

Create the PowerShell Function that adds the user to groups

The Add-UserToGroup function takes two parameters: $userId (the ID of the user) and $groupId (the ID of the group), attempts to add the user to the group using New-MgGroupMember cmdlet within the Try block. If an error occurs, it catches the error using the Catch block and displays a message in red.

Graph PowerShell function to add user to multiple groups

Loop through GroupIDs and run the PowerShell function that adds the user to groups

foreach loop iterates over each group ID in the $groupIds array and calls the Add-UserToGroup function to add the user to each group.

Looping through Group IDs and running Graph PowerShell function that adds user to groups

Entire Script


# Install the Microsoft Graph PowerShell module if not already installed
# Install-Module -Name Microsoft.Graph -Scope CurrentUser

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.ReadWrite.All"

# Define the user's UPN (User Principal Name) and the group IDs
$userUPN = "jacobdoe@7xh7fj.onmicrosoft.com"
$groupIds = @(
    "1cbe8c31-589d-453a-a1e5-045f7f00c967",
    "4a6c54df-9235-4854-8b98-5c0045c02855",
    "d2449eb1-db4a-4d87-83dd-988f7af420b1"
    # Add more group IDs as needed
)

# Get the user object
$user = Get-MgUser -UserId $userUPN

# Function to add a user to a group
function Add-UserToGroup {
    param (
        [string]$userId,
        [string]$groupId
    )
    
    try {
        New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
        Write-Host "Successfully added user to group with ID: $groupId" -ForegroundColor Green
    } catch {
        Write-Host "Failed to add user to group with ID: $groupId. Error: $_" -ForegroundColor Red
    }
}

# Loop through each group ID and add the user to the group
foreach ($groupId in $groupIds) {
    Add-UserToGroup -userId $user.Id -groupId $groupId
}

# Disconnect from Microsoft Graph
Disconnect-MgGraph

Write-Host "User has been added to all specified groups." -ForegroundColor Green
                            

Script Output

When you run the script, you should get "Successfully added user to group...." message as shown in the image.

Graph PowerShell script output confirming user added to groups

Related Articles:

Create Microsoft 365 Group Using Microsoft Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Get Microsoft 365 Group Owners List Using Graph PowerShell
Get Microsoft 365 Group Members Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex