A teacher requested private channels for each of their students in a Team so they could use them as digital portfolios and a way to keep track and communicate with each student.

Rather than spend the 15 minutes showing them how to create the private channels I spent an hour writing a script to automate the process.

The script below will take a csv file and create a private channel, then add the user, an admin, and another user as owner. I use a generic admin account and add that to all Teams so I have an easy way to access and troubleshoot any issues.

A few notes…

  1. You will need to have the preview version of Microsoft Teams installed
    Install-Module -Name MicrosoftTeams -AllowPrerelease
  2. The Team needs to be activated first, for some reason PowerShell can’t see the users unless it is activated.
  3. The CSV file must be formatted like the screenshot below.

Here is the script, be sure to edit the teamAdmin variable if you’d like to include an admin account on all of the private channels. If you don’t want to then comment out this line: Add-TeamChannelUser -GroupId $($teamsChannelArray[$count].groupId) -DisplayName $($teamsChannelArray[$count].channelTeam) -user $teamAdmin

# If you need to load a different file change it below here
$csv = "teamchannels.csv"

# Declare user array 
$teamsChannelArray = @()

# Global Team Admin - I have an account I add to all teams to make it easier to manage and troubleshoot issues
$teamAdmin = "teamsadmin@contoso.com"

# Import the CSV license file
$teamsChannelArray = Import-CSV $csv
$count = 0

# Step through the array and start creating private channels
foreach ($channel in $teamsChannelArray) {
Write-Host "Attempting to create private channel for: "$teamsChannelArray[$count].channelTeam
New-TeamChannel -GroupId $teamsChannelArray[$count].groupId -DisplayName $teamsChannelArray[$count].channelTeam -MembershipType Private -Owner $teamsChannelArray[$count].owner
Write-Host "Attempting to add student to: "$teamsChannelArray[$count].channelTeam
Write-Host "Student is: "$($teamsChannelArray[$count].user)""
Add-TeamChannelUser -GroupId $($teamsChannelArray[$count].groupId) -DisplayName $($teamsChannelArray[$count].channelTeam) -user $($teamsChannelArray[$count].user)
Write-Host "Attempting to add teams admin owner to: "$teamsChannelArray[$count].channelTeam
Add-TeamChannelUser -GroupId $($teamsChannelArray[$count].groupId) -DisplayName $($teamsChannelArray[$count].channelTeam) -user $teamAdmin
Write-Host -ForegroundColor Green "It probably worked"
# We've got a problem
Write-Host -ForegroundColor Red "Something broke and the team channel wasn't created for: "$teamsChannelArray[$count].channelTeam
Write-Host -ForegroundColor Red "Hopefully the error message below helps you fix it"

$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName

Write-Host "..." $ErrorMessage
Write-Host "..." $FailedItem
$count ++