Create a Microsoft Team with single/multiple owners/users using PowerShell
In this blog I will share one way to create a Microsoft Team with single/multiple owners/users PowerShell. Owners and users are added using either user’s full name or user’s UPN.
Introduction
My blogs have relatively simple, and sometimes complex, examples. I’m hoping that you will be able to tailor them to your need or use them in your own scripts.
The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, solution or process to accomplish a task.
Prerequisites
- The cmdlets for creating and managing teams are in the Microsoft Teams PowerShell module. First thing to remember is to install Install Microsoft Teams PowerShell Module. Otherwise, you can use Cloud Shell if you prefer to stay within Azure Portal.
Install-Module -Name MicrosoftTeams -Force
PowerShell Cmdlets
- Add-TeamUser This cmdlet adds an owner or member to the team, and to the unified group which backs the team.
- Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
- Connect-MicrosoftTeams This cmdlet connects an authenticated account for use with cmdlets from the MicrosoftTeams module.
- Get-AzADUser Filters active directory users.
- Get-Team This cmdlet supports retrieving teams with particular properties/information, including all teams that a specific user belongs to, all teams that have been archived, all teams with a specific display name, or all teams in the organization.
- New-Team This cmdlet lets you provision a new Team for use in Microsoft Teams and will create an O365 Unified Group to back the team. Groups created through teams cmdlets, APIs, or clients will not show up in Outlook by default.
- Remove-Team This cmdlet deletes a specified Team from Microsoft Teams. NOTE: The associated Office 365 Unified Group will also be removed.
Connect to Microsoft Teams
1 2 3 4 |
# If you have multiple subscription, make sure to use Set-AzContext # to set the context to the correct subscription. Connect-AzAccount Connect-MicrosoftTeams |
Set script variables
First, we will define variables to hold the new team values. Most of the times, the requester will provide users’ names and not users’ UPN. That is why I decided to include both in the Owners and Users variables.
1 2 3 4 5 6 7 8 9 |
$DisplayName = "SaadKhamis Marketing" $Description = "Collaboration space for Contoso's Marketing department" $Visibility = "Private" # or "Public" # Note: Multiple owners. Also, mix of user's UPN and full name $Owners = "user1@saadkhamis.com,User2 Khamis" # Note: Mix of user's UPN and full name $Users = "User3 Khamis,user4@saadkhamis.com, User5 Khamis,user6@saadkhamis.com" |
Function to get User’s UPN
Since variables have user’s full name, DisplayName, we have to convert that to user’s UPN using Get-AzADUser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Function Get-UserUpnS { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, HelpMessage="Enter user's display name")] [String] $UserDisplayName, [Parameter(Mandatory=$true, HelpMessage="Properties to be selected")] [Object] $AzADUser ) If ($UserDisplayName.IndexOf('@') -gt 0) { $UserUpn = $UserDisplayName } Else { $UserUpn = ($AzADUser | Where {$_.DisplayName -eq $UserDisplayName}).UserPrincipalName If ($UserUpn -eq $null) { Write-Host ('User account "' + $UserDisplayName + '" does not exist.') -ForegroundColor Red } } Return ($UserUpn) } # Get-UserUpnS $AzADUser = Get-AzADUser | Sort-Object Name # Included only to test the function Get-UserUpnS -UserDisplayName "Saad Khamis" -AzADUser $AzADUser # Included only to test the function Get-UserUpnS -UserDisplayName "Saad Khamis1" -AzADUser $AzADUser |
Does the Team already exist?
First, we will check if the team already exists. If it does, display an error message and stop.
1 2 3 4 5 |
$TeamObj = Get-Team | Where {$_.DisplayName -eq $DisplayName} If ($TeamObj -ne $null) { Write-Host ('Team "' + $DisplayName + '" already exists.') -ForegroundColor Red Break } |
Create new Team with single or multiple owners
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Convert $Owners string to an array, in case we need to add multiple owners $OwnersA = $Owners.Split(',') $OwnerUpn = Get-UserUpnS -UserDisplayName $OwnersA[0] -AzADUser $AzADUser Write-Host ('Creating new team "' + $DisplayName + '"') $TeamObj = New-Team -DisplayName $DisplayName -Description $Description -Visibility $Visibility -Owner $OwnerUpn # Add additional owners, if required If ($OwnersA.Count -gt 1) { For ($i = 1; $i -lt $OwnersA.Count; $i++) { $Owner = $OwnersA[$i].TrimStart(" ") $OwnerUpn = Get-UserUpnS -UserDisplayName $Owner -AzADUser $AzADUser If ($OwnerUpn -ne $null) { Write-Host ('Adding additional owner "' + $Owner + '"') Add-TeamUser -GroupId $TeamObj.GroupId -User $OwnerUpn -Role Owner } } } |
Add single or multiple users/members
1 2 3 4 5 6 7 8 9 |
$UsersA = $Users.Split(",") ForEach ($User in $UsersA) { $User = $User.TrimStart(" ") $UserUpn = Get-UserUpnS -UserDisplayName $User -AzADUser $AzADUser If ($UserUpn -ne $null) { Write-Host ('Adding user "' + $User + '"') Add-TeamUser -GroupId $TeamObj.GroupId -User $UserUpn -Role Member } } |
Without delay, our complete amazing Script
Putting all the pieces together produce a nice script. What’s more, you can add convert the script to a function and add it to your library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
Connect-MicrosoftTeams $DisplayName = "SaadKhamis Marketing" $Description = "Collaboration space for Contoso's Marketing department" $Visibility = "Private" # or "Public" # Note: Multiple owners, mix of user's UPN and full name and space after the comma $Owners = "User1@saadkhamis.com, User2@saadkhamis.com" # Note: Mix of user's UPN and full name $Users = "User4@saadkhamis.com, User5 Khamis" Function Get-UserUpnS { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, HelpMessage="Enter user's display name")] [String] $UserDisplayName, [Parameter(Mandatory=$true, HelpMessage="Properties to be selected")] [Object] $AzADUser ) If ($UserDisplayName.IndexOf('@') -gt 0) { $UserUpn = $UserDisplayName } Else { $UserUpn = ($AzADUser | Where {$_.DisplayName -eq $UserDisplayName}).UserPrincipalName If ($UserUpn -eq $null) { Write-Host ('User account "' + $UserDisplayName + '" does not exist.') -ForegroundColor Red } } Return ($UserUpn) } # Get-UserUpnS # Existing Team $TeamObj = Get-Team | Where {$_.DisplayName -eq $DisplayName} If ($TeamObj -ne $null) { Write-Host ('Team "' + $DisplayName + '" already exists.') -ForegroundColor Red Break } $AzADUser = Get-AzADUser | Sort-Object Name # Convert $Owners string to an array, in case we need to add multiple owners $OwnersA = $Owners.Split(',') $OwnerUpn = Get-UserUpnS -UserDisplayName $OwnersA[0] -AzADUser $AzADUser Write-Host ('Creating new team "' + $DisplayName + '"') $TeamObj = New-Team -DisplayName $DisplayName -Description $Description -Visibility $Visibility -Owner $OwnerUpn # Add additional owners, if required If ($OwnersA.Count -gt 1) { For ($i = 1; $i -lt $OwnersA.Count; $i++) { $Owner = $OwnersA[$i].TrimStart(" ") $OwnerUpn = Get-UserUpnS -UserDisplayName $Owner -AzADUser $AzADUser If ($OwnerUpn -ne $null) { Write-Host ('Adding additional owner "' + $Owner + '"') Add-TeamUser -GroupId $TeamObj.GroupId -User $OwnerUpn -Role Owner } } } $UsersA = $Users.Split(",") ForEach ($User in $UsersA) { $User = $User.TrimStart(" ") $UserUpn = Get-UserUpnS -UserDisplayName $User -AzADUser $AzADUser If ($UserUpn -ne $null) { Write-Host ('Adding user "' + $User + '"') Add-TeamUser -GroupId $TeamObj.GroupId -User $UserUpn -Role Member } } |
Putting our amazing script to work
Example to illustrate the usage of our amazing script.
1 2 3 4 5 6 7 8 9 10 11 12 |
Connect-MicrosoftTeams Connect-AzAccount $DisplayName = "Test skhamis" $Description = "Collaboration space for Contoso's Marketing department" $Visibility = "Private" # or "Public" # Note: Multiple owners. Also, mix of user's UPN and full name $Owners = "s11khamis@swapa.org,Saad7 Test Khamis,Saad0 Test Khamis" # Note: Mix of user's UPN and full name $Users = "s12khamis@swapa.org, Chris2 Test Howell, User3 Khamis" |
Clean up resources
1 |
Remove-Team -GroupId $TeamObj.GroupId |
In conclusion
In summary, we explored writing a script to create a Microsoft Team using PowerShell including adding a single or multiple owners and users. Owners and users are added using either user’s full name or user’s UPN.
Did you find this blog easy to follow and helpful to you? I certainly would love to hear your feedback and suggestions. So, let me know in the comments below. Happy PowerShelling.
Disclaimer
Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
There's no comments