PowerShell Script Sample - Create a company-wide team in Microsoft Teams

This PowerShell script creates a public, company-wide team in Teams. The script uses the MicrosoftTeams PowerShell module (currently in beta) to create a public, company-wide team. It also uses the AzureAD PowerShell module to fetch the list of users in your tenant.

For complete instructions on using this PowerShell script, check out our tutorial, Create a company-wide team in Teams using PowerShell.

If you're new to PowerShell and need help getting started, see Overview of Azure PowerShell.

Sample script


A PowerShell script file is a text file with a .PS1 extension. To use this script, click Copy and then paste the code into a text file. Save it with the filename CreateCompanyWideTeam.ps1, and you've got a PowerShell script.

This script allows you to create a company-wide team.
Use this script with the MicrosoftTeams PowerShell version 0.9.0 beta module to create a team with members and channels that you specify. Also leverages the AzureAD module to find all members to add.
The comma-separated list of owners for your Team, who handle team settings and membership management. The owners should be specified by their User Principal Name. You must specify a minimum of two owners.
The name of your company-wide team. For example, this could be your company name.
.PARAMETER TeamDescription
The description of your company-wide team, in quotes.
The comma-separated list of names of all the initial channels you want to set up. For example: "Announcements","Social at Work","Teamwork at Contoso".
If you have already created the group for your company-wide team, specify its GroupID. You can get this value from the display of the “Get-AzureADGroup | Sort DisplayName | Select DisplayName,ObjectID” command. This script had an issue during the rest of the execution and stopped early. 
The comma-separated list of members for your team that you want to manually add. To use this script a minimum of two members must be added. The members should be specified by their User Principal Name. To manually just add a single member, use the “Add-TeamUser -GroupId $IdOfGroup -Role Member -User $member” command.

function Create-Team(){
    Write-Host("Starting Team Creation")
    $GroupID = (New-Team -DisplayName $TeamName -AccessType Public -Description $TeamDescription).GroupId

        Write-Host("Team creation failed, please investigate and try again")

    Write-Host("Team GroupID:", $GroupID)

    return ,$GroupID

function Add-Channels($IdOfGroup){
        Write-Host("Starting Channel addition")
    foreach($channel in $Channels)
            Write-Host("Enter Channel Description for " + $channel + " channel")
            $channelDescription = Read-Host
            New-TeamChannel -GroupId $IdOfGroup -DisplayName $channel -Description $channelDescription

function Add-Members ($IdOfGroup){
    Write-Host("Starting Member addition")
    $allUsersInTenant = $Members 

    ## Only fetch full user list, if one isn't supplied

        Write-Host("Starting connection to AzureAD module") 

            Write-Host("Unable to connect to AzureAD. Please investigate and try again. To install the commandlet, use this command: Install-Module AzureAD")

       ## Fetch all the users and create the member list
        $allUsersInTenant = (Get-AzureADUser).UserPrincipalName 


    $NotAddedMembers = [System.Collections.ArrayList]@()
    $existingTeamMembers = (Get-TeamUser -GroupId $IdOfGroup).User

    foreach($user in $allUsersInTenant){


                Add-TeamUser -GroupId $IdOfGroup -Role Owner -User $user.ToString()

                Add-TeamUser -GroupId $IdOfGroup -Role Member -User $user.ToString()

                if($Error) {

    if($NotAddedMembers.Count > 0) {
        Write-Host("There are some members that did not get added, please retry these specific users")

    ##Clear member addition errors

##Main Script
Write-Host("Starting connection to MicrosoftTeams module") 


    Write-Host("There was an error installing\connecting to the Microsoft Teams PowerShell module. Please investigate. To install the module use the follow command: Install-Module MicrosoftTeams ")

##Create the team if existing GroupID isn't supplied. It's possible a Tenant Wide team was created, but there was an error adding members.

        Write-Host("Missing an argument for parameter 'TeamName' and 'GroupID'. Specify a parameter of type 'System.String' and try again for either GroupID or TeamName and TeamDescription")
    $GroupID = Create-Team

if($Channels.Count -gt 0){
    Add-Channels $GroupID

Add-Members $GroupID 


Script explanation

For complete instructions on using this PowerShell script, check out our tutorial, Create a company-wide team in Teams using PowerShell

There are 5 sections to this script (listed in order from top to bottom):

  1. Documentation and variable definitions
  2. Function Create-Team: Creates a team if necessary. If you don't specify a GroupID, this section will create the team with the specified TeamName and TeamDescription.
  3. Function Add-Channels: If you've specified channels, this is where the channel description gets requested, and then the channel gets created.
  4. Function Add-Members: This does all member management. This section connects to AzureAD, fetches a set of users, and adds them to the team if you don’t provide a member list. It handles the logic of adding owners and members, and not adding members who already belong to the team.
  5. Main program: This is the order in which the functions get executed.