Adding members to a Microsoft Team can be a routine but critical task, especially for organizations that need to manage dynamic teams efficiently. This PowerShell script provides a streamlined, interactive console-based approach to adding members to a team using the Microsoft Graph API. Whether adding a single user, multiple users, or bulk-importing from a CSV file, this script simplifies the process and allows IT admins to save valuable time.
# Microsoft Graph API connection
Connect-MgGraph -Scopes "Group.ReadWrite.All"
Write-Host "Connected to Microsoft Graph"
# Function to add a single member to a team
function Add-SingleMember {
$teamId = Read-Host "Enter the Team ID"
$userId = Read-Host "Enter the User Principal Name (email) or User ID of the member to add"
$user = Get-MgUser -UserId $userId
if ($user) {
New-MgGroupMember -GroupId $teamId -DirectoryObjectId $user.Id
Write-Host "Member $userId has been added to the team."
} else {
Write-Host "User $userId not found."
}
}
# Function to add multiple members to a team
function Add-MultipleMembers {
$teamId = Read-Host "Enter the Team ID"
$userPrincipalNames = Read-Host "Enter User Principal Names (emails) separated by commas"
$userList = $userPrincipalNames -split ","
foreach ($userId in $userList) {
$user = Get-MgUser -UserId $userId.Trim()
if ($user) {
New-MgGroupMember -GroupId $teamId -DirectoryObjectId $user.Id
Write-Host "Member $userId has been added to the team."
} else {
Write-Host "User $userId not found."
}
}
}
# Function to bulk add members to a team from a CSV file
function Bulk-ImportMembers {
$teamId = Read-Host "Enter the Team ID"
$filePath = Read-Host "Enter the CSV file path (the file should contain a 'UserPrincipalName' column)"
if (Test-Path $filePath) {
$csvData = Import-Csv -Path $filePath
foreach ($row in $csvData) {
$userId = $row.UserPrincipalName
$user = Get-MgUser -UserId $userId
if ($user) {
New-MgGroupMember -GroupId $teamId -DirectoryObjectId $user.Id
Write-Host "Member $userId has been added to the team."
} else {
Write-Host "User $userId not found."
}
}
} else {
Write-Host "The specified CSV file path does not exist."
}
}
# Main Script Loop
while ($true) {
Write-Host "`nChoose an action to add team members:"
Write-Host "1. Add a single member"
Write-Host "2. Add multiple members"
Write-Host "3. Bulk import members from CSV"
Write-Host "4. Exit"
$choice = Read-Host "Enter the number corresponding to your choice"
switch ($choice) {
"1" { Add-SingleMember }
"2" { Add-MultipleMembers }
"3" { Bulk-ImportMembers }
"4" { Write-Host "Exiting..."; break }
default { Write-Host "Invalid choice, please try again." }
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Disconnected from Microsoft Graph"
See the Script in Action by clicking and playing these GIFs:
This script provides an interactive way for administrators to manage Microsoft 365 Team members. Here's how the script works:
There are several ways to further enhance this script for more robust Microsoft 365 Teams management:
# Verify User
Get-MgUser -UserId "user@domain.com"
This PowerShell script is an effective tool for managing team members in Microsoft Teams. It simplifies the process of adding users, whether one at a time, in groups, or through bulk imports from a CSV file. With the ability to scale, automate, and customize this process, admins can save valuable time and reduce the chances of error when managing team memberships. With additional enhancements like improved error handling and logging, this script becomes a powerful solution for efficient and error-free team management.
© m365corner.com. All Rights Reserved. Design by HTML Codex