Microsoft Teams : Governance – Check for duplicate Teams in a Tenant and Create bulk Teams and add Owners, Members through PowerShell

.

No one is you and that is your power.

.

Hello Everyone,

Hope you all are doing well.

we have good list of articles on Microsoft Teams using PowerShell, please have a look.

https://knowledge-junction.in/category/technology-articles/powershell-cmdlets/teams-powershell/

In this article we are going to discuss how to check duplicate Teams in a Tenant and create bulk Teams and add owners and members from CSV file through PowerShell.

So without getting late, let’s get started.

Background

In our organization, one of our project requirement is to identify the duplicate Teams in a Tenant and create new Teams in bulk and add owners and members to their respective Teams using PowerShell script.

Details

  • We need to prepare the Teams list in a CSV format and saved in a preferred location.
  • We can use Microsoft Excel , to prepare the bulk teams list, then save as CSV.
  • CSV file with headings and details to create bulk teams.
Csv file
fig: CSV file
  • Separate the members in a row with “;” in CSV file.
  • Open PowerShell ISE.
  • Now ran the PowerShell with the following cmdlet to add create bulk Teams.

Note: The line starting with “#” are just description heading or comment.

Prerequisites

  • Install PowerShell ISE / PowerShell / Visual Studio Code
  • Install Module Microsoft Teams
  • Prepare CSV file with required details to create bulk Teams.
# Install Microsoft Teams if not installed
Install-Module Microsoft Teams

.

Detailed Steps – PowerShell script

  • Import Microsoft Teams
# Import Microsoft Teams
Import-Module Microsoft Teams
  • Connect to the Microsoft Teams.
# Connect Microsoft Teams of our Tenant
Connect-MicrosoftTeams
  • Import the prepared CSV file and use pipeline to send the results of CSV.
#Import CSV
Import-CSV "E:\PowerShellScripts\bulkteams.csv" | 
  • Apply a loop – for each to read row by row of CSV file.
foreach {}
  • Set a variable for display name of teams.
#Set team display name in a variable
$teamDisplayName = $_.TeamDisplayName ;
  • Get the teams in a organization with display name and check with CSV file display name row by row.
#Get teams with same display name
$TeamID = Get-Team | Where {$_.DisplayName -eq $teamDisplayName} | Select -ExpandProperty GroupID 
  • Condition to check for existing teams.
#Condition for null team id
If ($TeamID -eq $null)
  • If we have existing teams with same display name then skip creating new team for the same.
  • If we don’t have the existing teams, create a new Team and add the details as per CSV.
#Create team and add owner
$group = New-Team -DisplayName $_.TeamDisplayName -Owner $_.Owner -Visibility $_.Visibility
  • Set the members of teams in a variable and use split to separate the members in a row.
#Set members in a variable
$users = $_.Members.Split(";")
  • Apply a loop for each member in a row.
#Apply for each user
foreach ($user in $users)
  • Add the members to the team
#Add users to the team
Add-TeamUser -GroupId $group.GroupId -User $user
  • Finally we created the bulk teams and added owners, members to the respective teams.

.

Complete Script

<#
===================================================================================================================================================
Name:           Check for duplicate Teams in a Tenant and Create bulk Teams and add Owners, Members through PowerShell
Description:    This script helps to check duplicate Teams in a Tenant and create bulk Teams and add owners, members in a Tenant through PowerShell
Version:        1.0
===================================================================================================================================================
#>

 #Install Microsoft Teams
#Install-Module Microsoft Teams

 #Import Microsoft Teams
#Import-Module Microsoft Teams

 #Connect to Microsoft Teams
Connect-MicrosoftTeams

 #Import Csv
Import-Csv "E:\PowerShellScripts\bulkteams.csv" | 

foreach {
             #Set team display name in a variable
            $teamDisplayName = $_.TeamDisplayName ; 

             #Get teams with same display name
            $TeamID = Get-Team | Where {$_.DisplayName -eq $teamDisplayName} | Select -ExpandProperty GroupID 

             #Print the team id having same display same
            Write-Host "TeamID" $TeamID

             #Condition for null team id
            If ($TeamID -eq $null)

            {
                 #Create team and add owner
                $group = New-Team -DisplayName $_.TeamDisplayName -Owner $_.Owner -Visibility $_.Visibility

                 #Set members in a variable
                $users = $_.Members.Split(";")

                 #Apply for each user
                foreach ($user in $users)
        
                {
                     #Add users to the team
                    Add-TeamUser -GroupId $group.GroupId -User $user
                }

            }

        } #foreach

.

Hope this article will help us to check for duplicate Teams in a Tenant and create bulk Teams and add owners and members in a Tenant from CSV through PowerShell.

Also get my article updates on my social media handles.

LinkedIn – https://www.linkedin.com/in/khasim-shaik-8784a1232/

Twitter – https://twitter.com/KhasimShaik2009

Facebook – https://www.facebook.com/profile.php?id=100078255554660

Thank you for your support, will catch up with new article soon.

Keep learning and keep smiling.

Thanks.

Khasim Shaik

SharePoint & Power Platform Developer at OS InfoTech

You may also like...

1 Response

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: