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.
If you haven't already, install the Graph PowerShell module using the following PowerShell command.
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
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.
Run Get-MgGroup command to get the IDs of the Microsoft 365 Groups you wish to add the user to.
Run Get-MgUser command and get the UserPrincipalName of the user who is going to be added to the Microsoft 365 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.
foreach loop iterates over each group ID in the $groupIds array and calls the Add-UserToGroup function to add the user to each group.
# 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
When you run the script, you should get "Successfully added user to group...." message as shown in the image.
© m365corner.com. All Rights Reserved. Design by HTML Codex