Import M365 Users to Microsoft Teams from CSV file
The Graph PowerShell script helps you import Microsoft 365 users from CSV files into Microsoft Teams. The script reads user IDs from a CSV file and adds these users into the designated Team. Once the users are successfully added, it prints out a success message.
Identify the Microsoft Team ID
Run the following script to identify the Microsoft Team's ID to which M365 users are to be imported. The script fetches IDs of only the Teams-enabled Microsoft 365 groups. You will need this ID while importing users.
- Get-MgGroup cmdlet retrieves all Microsoft 365 groups.
- The -Filter parameter applies a filter to only select groups that are Microsoft Teams. The filter "resourceProvisioningOptions/Any(x:x eq 'Team')" checks the resourceProvisioningOptions property of each group to determine if any of the entries equals 'Team'. This property identifies the services that are enabled for a group, and in this case, it filters for groups where Microsoft Teams is enabled.
Create the CSV file with User Details
Create a CSV file that lists all the User IDs you want to add to the Microsoft Team. Set the CSV header as UserId.
Example CSV File Structure
DirectoryObjectId
0c96a46b-df04-4b05-b038-083b5058531b,
0c96a46b-df04-4b05-b038-083b5058531b,
0c96a46b-df04-4b05-b038-083b5058531b,
Notes:
- You can get the User IDs of the users to be added to the group by running Get-MgUser cmdlet.
Create the Graph PowerShell Script
Create a script to read the CSV file and add users to the specified Microsoft 365 group. The script makes use of Import-Csv to import the users from the CSV file and New-MgGroupMember cmdlet to add the users to the selected Microsoft 365 group.
Import-CSV "path_to_CSV_file" | ForEach {
try{
# Attempt to add a new group member
$newMember = New-MgGroupMember -GroupId "0097f95d-63b4-48c5-a0ca-310578e2cfdb" -DirectoryObjectId $_.UserID
#Output success message to the console
Write-Output "Successfully added user with ID ($_.UserID)"
}catch{
# Output error message to the console
Write-Output "Failed to add user with ID $($_.UserID) to group. Error: $_"
}
}
When you execute the script, you should get the following response:
How the Script Works?
The Microsoft 365 Teams User Import PowerShell script does the following:
- Reads the CSV file using the Import-Csv cmdlet. Passes on the file's content to Foreach cmdlet.
- The Foreach cmdlet loops through the User IDs and executes the New-MgGroupMember cmdlet.
- The New-MgGroupMember cmdlet takes in the User Id and the Team's Id (you obtained earlier) and adds the user to the team.
- Finally, a success message is printed on the PowerShell console.
- If there are any errors - like the one shown in the output which complains about member already being present in the group - the catch block prints it on the PowerShell console.
Further Enhancing the Script
Here are some suggestions on how you can enhance the script for better performance, error handling, and functionality:
- Logging to a File: Instead of or in addition to displaying output on the console, you might want to log the results of each operation to a file. This can help with record-keeping and troubleshooting later.
- Using Custom Objects for Output: Create custom PowerShell objects to structure the output data, which can then be exported to a CSV file for easy analysis.
- Parameterization: To make the script more reusable, consider parameterizing the script. This makes it easier to run the script with different groups or CSV files without modifying the script itself.
- Rate Limiting and Error Recovery: If you're working with a large CSV file, the script might hit API rate limits. Introducing retries or delays can help manage this.
These enhancements can significantly improve the script's functionality, making it more robust and adaptable for various scenarios.
📄 CSV Structure Matters
Ensure your CSV file includes headers like UserPrincipalName
and Role
. Proper formatting prevents import errors and ensures members and owners are added correctly to Microsoft Teams.
🛠️ Use Object IDs to Avoid Lookup Failures
While UserPrincipalName
works in many cases, using the user's unique Id
(GUID) is more reliable—especially in hybrid or synced environments where display names might be ambiguous or duplicated.
Related Articles:
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell