Forum Discussion
Luca VALENTINO
Mar 21, 2020Copper Contributor
Create Teams from csv
I am trying to create Teams from a CSV file My Csv: Mail TeamsName TeamsDescription Type TypeTemplate Owners EV0123 EV - Prova Descrizione1 Private EDU_Class email@dom.it SN0123 ...
MariusPretorius
Nov 25, 2020Steel Contributor
I am successfully using the script from Create teams in Microsoft Teams through CSV using PowerShell | JiJi Technologies to create multiple teams, including the channels and adding members simultaneously, from a prepared CSV file.
N.B. I made two small changes to the script.
- Preparation:
- I ran PowerShell in Admin mode
- I ensured that I had installed the current version of the MicrosoftTeams PowerShell module. I ran the following command in PowerShell (in admin mode) Install-Module -Name MicrosoftTeams -RequiredVersion 1.1.6
- I made the two highlighted changes to script in the function Create-NewTeam:
- $group = New-Team -MailNickName $team.TeamsName -displayname $team.TeamsName -Template $team.TeamType
- In the original script it uses -Alias instead of -MailNickName and -AccessType instead of -Template
- My CSV file had the following headers and example content
TeamsName TeamType ChannelName Owners Members 21Accounting11 EDU_class Channel1; Channel2 Teacher1@domain.com Member1@domain.com; member2@domain.com - The Team Type is important - you need that in an educational setting to create Class type teams.
RicMercene
Apr 14, 2021Copper Contributor
MariusPretorius Luca VALENTINO EDU_ictGeek
Good day!
I really need your help... please....
I tried your codes but nothing happened....
attached is my .csv file for reference and below (here) is my code...
function Create-Channel
{
param (
$ChannelName,$GroupId
)
Process
{
try
{
$teamchannels = $ChannelName -split ";"
if($teamchannels)
{
for($i =0; $i -le ($teamchannels.count - 1) ; $i++)
{
New-TeamChannel -GroupId $GroupId -DisplayName
$teamchannels[$i]
}
}
}
Catch
{
}
}
}
function Add-Users
{
param(
$Users,$GroupId,$CurrentUsername,$Role
)
Process
{
try{
$teamusers = $Users -split ";"
if($teamusers)
{
for($j =0; $j -le ($teamusers.count - 1) ; $j++)
{
if($teamusers[$j] -ne $CurrentUsername)
{
Add-TeamUser -GroupId $GroupId -User
$teamusers[$j] -Role $Role
}
}
}
}
Catch
{
}
}
}
function Create-NewTeam
{
param (
$ImportPath
)
Process
{
Import-Module MicrosoftTeams
$cred = Get-Credential
$username = $cred.UserName
Connect-MicrosoftTeams -Credential $cred
$teams = Import-Csv -Path $ImportPath
foreach($team in $teams)
{
$getteam= get-team |where-object { $_.displayname -eq
$team.TeamsName}
If($getteam -eq $null)
{
Write-Host "Start creating the team: " $team.TeamsName
$group = New-Team -DisplayName $team.TeamsName -
displayname $team.TeamsName -Template $team.TeamType
Write-Host "Creating channels..."
Create-Channel -ChannelName $team.ChannelName -GroupId
$group.GroupId
Write-Host "Adding team members..."
Add-Users -Users $team.Members -GroupId $group.GroupId -
CurrentUsername $username -Role Member
Write-Host "Adding team owners..."
Add-Users -Users $team.Owners -GroupId $group.GroupId -
CurrentUsername $username -Role Owner
Write-Host "Completed creating the team: "
$team.TeamsName
$team=$null
}
}
}
}
Create-NewTeam -ImportPath "C:\Users\Admin1234\Desktop\1.csv"
Thank you very much. Best Regards.
- MariusPretoriusApr 26, 2021Steel Contributor
It seems your code is missing a function name and opening curly bracket at the beginning and a closing curly bracket below your last line. Your code looks like this:
function Create-Channel
{
..
.
Create-NewTeam -ImportPath "C:\Users\Admin1234\Desktop\1.csv"
It should read like this: (I'm only including the missing code)
Function New-TeamsFromCSV { function Create-Channel . . . Create-NewTeam -ImportPath "C:\Users\Admin1234\Desktop\1.csv" }
Obviously, you have to take a few steps for your code to execute in PowerShell:
- you need to use the PowerShell command line to import the module
import-module TheModuleName.psm1
- I prefer to run PowerShell in Administrator mode to make sure I don't run into permissions problems
- The name of the module you need to import is the (path and the )name of your .psm1 file
- and call the function (run it) as shown below.
New-TeamsFromCSV
Because you are connecting to Teams Admin, you will be asked for your credentials. You need to be a Teams Administrator and provide that username/email address and password. Thereafter you will be flying!
- RicMerceneApr 27, 2021Copper Contributor
Good day!
I already tried your code but this is the error message i've received...
below is the whole code...
----
Function New-TeamsFromCSV
{
function Create-Channel
{
param (
$ChannelName,$GroupId
)
Process
{
try
{
$teamchannels = $ChannelName -split ";"
if($teamchannels)
{
for($i =0; $i -le ($teamchannels.count - 1) ; $i++)
{
New-TeamChannel -GroupId $GroupId -DisplayName$teamchannels[$i]
}
}
}
Catch
{
}
}
}function Add-Users
{
param(
$Users,$GroupId,$CurrentUsername,$Role
)
Process
{
try{
$teamusers = $Users -split ";"
if($teamusers)
{
for($j =0; $j -le ($teamusers.count - 1) ; $j++)
{
if($teamusers[$j] -ne $CurrentUsername)
{
Add-TeamUser -GroupId $GroupId -User$teamusers[$j] -Role $Role
}
}
}
}
Catch
{
}
}
}function Create-NewTeam
{
param (
$ImportPath
)
Process
{
Import-Module MicrosoftTeams
$cred = Get-Credential
$username = $cred.UserName
Connect-MicrosoftTeams -Credential $cred
$teams = Import-Csv -Path $ImportPath
foreach($team in $teams)
{
$getteam= get-team |where-object { $_.displayname -eq$team.TeamsName}
If($getteam -eq $null)
{
Write-Host "Start creating the team: " $team.TeamsName
$group = New-Team -DisplayName $team.TeamsName -displayname $team.TeamsName -Template $team.TeamType
Write-Host "Creating channels..."
Create-Channel -ChannelName $team.ChannelName -GroupId$group.GroupId
Write-Host "Adding team members..."
Add-Users -Users $team.Members -GroupId $group.GroupId -CurrentUsername $username -Role Member
Write-Host "Adding team owners..."
Add-Users -Users $team.Owners -GroupId $group.GroupId -CurrentUsername $username -Role Owner
Write-Host "Completed creating the team: "$team.TeamsName
$team=$null
}
}
}
}Create-NewTeam -ImportPath "C:\Users\Admin1234\Desktop\NewTeams
\NewTeams.csv"
}
- RicMerceneApr 27, 2021Copper Contributor
Good day!
Thank you for your response.... I will definitely study and try your program and instruction...
Cheers!
Best Regards. 🙂
- MariusPretoriusApr 26, 2021Steel Contributor
Please see my post here (Creating bulk class teams from a CSV file). It has the PowerShell module as well as a sample CSV file.
- wardt9May 21, 2021Copper ContributorThanks for this, it worked well, the only issue I ran into is that I needed to update my MicrosoftTeams Powershell Module to the latest version. Prior to that then it would run successfully once per day but then after that, fail with an error about MS Graph backend. Anyway, the update resolved this. So thanks again!