This guide demonstrates how to use the Add-MgTeamMember cmdlet in Microsoft Graph PowerShell to add members to Microsoft Teams. Learn how to manage team membership, add owners or members, and handle bulk operations efficiently.
The Add-MgTeamMember
cmdlet allows you to add members to a Microsoft Team by providing a team ID and details about the users being added, defined through the -BodyParameter
. This cmdlet is highly versatile and can be used for adding individual users, multiple users, or performing bulk user additions via a CSV file. This article provides detailed examples for each scenario and addresses potential errors administrators may encounter while using this cmdlet.
Note New-MgTeamMember is the alternative to Add-MgTeamMember cmdlet. You can also use Invoke-MgGraphRequest to Add Microsoft Team Member
Add-MgTeamMember -TeamId <String> -BodyParameter <Hashtable>
$teamId = "your-team-id"
$params = @{
values = @(
@{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
roles = @() # No specific role assigned (member by default)
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('user-id')"
}
)
}
Add-MgTeamMember -TeamId $teamId -BodyParameter $params
Note: You might get an error message, but the team member will get added to the team.
$teamId = "your-team-id"
$params = @{
values = @(
@{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
roles = @("owner") # Assigning user as an owner
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('owner-user-id')"
}
@{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
roles = @() # Assigning another user as a member
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('member-user-id')"
}
)
}
Add-MgTeamMember -TeamId $teamId -BodyParameter $params
Note: You might get an error message, but the team member will get added to the team.
If you need to add many users to a team, you can automate the process by reading from a CSV file. The CSV file should contain user IDs and their roles.
UserId,Role
user1-id,owner
user2-id,member
user3-id,member
PowerShell Script to Add Users in Bulk:
$teamId = "your-team-id"
$csv = Import-Csv -Path "C:\Path\To\TeamMembers.csv"
$values = @()
foreach ($row in $csv) {
$role = if ($row.Role -eq 'owner') { @("owner") } else { @() }
$values += @{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
roles = $role
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('$($row.UserId)')"
}
}
$params = @{ values = $values }
Add-MgTeamMember -TeamId $teamId -BodyParameter $params
user@odata.bind
property must contain the URL format for the user. Incorrect formatting will result in errors.Error | Cause | Solution |
ResourceNotFound | The provided team ID does not exist or is incorrect. | Double-check the team ID by running Get-MgTeam to ensure the team exists and is accessible. |
Request_ResourceNotFound | The user specified in the user@odata.bind URL does not exist or is incorrect. |
Verify that the user exists by running Get-MgUser . Ensure the user ID in the URL is correct and properly formatted. |
Forbidden | The account executing the command lacks the necessary permissions. | Ensure that the executing user has sufficient Microsoft Teams administrator or member addition permissions. You may need to assign more Graph API permissions to the account. |
InvalidRequest | The BodyParameter hashtable is improperly formatted. |
Ensure that the format of the @odata.type and user@odata.bind properties follows the exact structure specified in the Microsoft documentation. |
What is Add-MgTeamMember used for?
Add-MgTeamMember is a Microsoft Graph PowerShell cmdlet used to add users as members or owners to a Microsoft Team. It supports assigning roles and managing team membership.
How can I add a single user to a team as a member?
Use the following script to add a user as a team member.
$Body = @{
"@odata.type" = "microsoft.graph.aadUserConversationMember"
roles = @()
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('<UserObjectId>')"
}
Add-MgTeamMember -TeamId "<TeamId>" -BodyParameter $Body
How can I add multiple users to a team using a CSV file?
Prepare a CSV file with the following format:
TeamId,UserObjectId,Role
<YourTeamId1>,<UserObjectId1>,member
<YourTeamId1>,<UserObjectId2>,owner
Use this script to process the CSV and add users to teams:
$Data = Import-Csv -Path "C:\Path\To\File.csv"
foreach ($Row in $Data) {
$Body = @{
"@odata.type" = "microsoft.graph.aadUserConversationMember"
roles = if ($Row.Role -eq "owner") { @("owner") } else { @() }
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('$($Row.UserObjectId)')"
}
Add-MgTeamMember -TeamId $Row.TeamId -BodyParameter $Body
}
What permissions are required to add members to a team?
You need the TeamMember.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure delegated or application permissions are consented before running the cmdlet.
The Add-MgTeamMember
cmdlet is a powerful tool for automating member management in Microsoft Teams. Whether you need to add a single user, multiple users, or perform bulk additions via a CSV file, this cmdlet simplifies the process. By carefully formatting the BodyParameter
and ensuring correct permissions, you can avoid common errors and achieve efficient team member management.
By integrating this cmdlet into your automation workflows, you can significantly reduce manual effort and streamline Microsoft Teams management in your organization.
© m365corner.com. All Rights Reserved. Design by HTML Codex