Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more β all from one place.
π Launch ToolkitManaging team memberships manually can be tedious, especially when onboarding multiple users across departments or projects. With Microsoft Graph PowerShell, you can automate the process and bulk add members to Teams using just a CSV file. This article walks you through the process with a simple, reliable script.
# Connect to Microsoft Graph with necessary permissions
Connect-MgGraph -Scopes "TeamMember.ReadWrite.All"
# Import the CSV file
$members = Import-Csv -Path "members.csv"
foreach ($entry in $members) {
$teamId = $entry.TeamId
$userId = $entry.UserId
# Prepare body parameters for each member
$params = @{
"@odata.type" = "#microsoft.graph.aadUserConversationMember"
roles = @() # All users added as members
"user@odata.bind" = "https://graph.microsoft.com/v1.0/users('$userId')"
}
try {
New-MgTeamMember -TeamId $teamId -BodyParameter $params
Write-Host "β
Added user '$userId' to team '$teamId'" -ForegroundColor Green
} catch {
Write-Host "β Failed to add user '$userId': $_" -ForegroundColor Red
}
}
TeamId,UserId
7c564b30-bb67-48df-b7cb-cfb60f2cf5a3,fa25cbd9-bbe4-4d1a-aaa3-e7f7c23828b5
7c564b30-bb67-48df-b7cb-cfb60f2cf5a3,20f85e5b-813a-4b75-83f5-69d15b7cf437
Use Get-MgTeam and Get-MgUser to fetch these values.
You can extend this script to support:
Enhancement | Description |
Retry Logic | Handle transient errors (e.g., throttling) with retry attempts |
Add Owners | Accept Role column and assign "owner" if needed |
Logging | Export success/failure logs to a separate CSV |
UPN Support | Accept UserPrincipalName instead of UserId and resolve it dynamically |
License Check | Ensure users being added have proper licenses to access Teams |
Error Message | Cause | Fix |
---|---|---|
Insufficient privileges to complete the operation. | Missing delegated permissions | Ensure you're using MgGraph -Scopes "TeamMember.ReadWrite.All" |
InvalidAuthenticationToken | Expired token or session | Run Connect-MgGraph again to refresh the authentication token. |
BadRequest: Unable to cast object... | Improper or missing user@odata.bind format | Use the format: "user@odata.bind" "https://graph.microsoft.com/v1.0/users('USER-ID')" |
User not found | Invalid or deleted UserId in the CSV | Use Get-MgUser to verify user exists before import |
The specified object is already a member | Duplicate add attempt | Add a pre-check to skip users already in the team (optional enhancement) |
This Graph PowerShell script provides a fast, efficient, and repeatable way to bulk import Microsoft Teams members using a simple CSV file. It's ideal for IT admins looking to streamline onboarding, migrations, or periodic updates to team memberships.
As Microsoft Teams adoption continues to grow, automation tools like this can significantly reduce manual effort and help maintain consistency across your environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex