Teams PowerShell

Like many of the app that makes up the Office 365 suite, you have the option to manage Microsoft Teams through its Admin Center or using PowerShelI. You can manage everything from adding users and groups to managing policies of said users and groups.  In this article, you will learn the most common Microsoft Teams Powershell Commands to help manage your Microsoft Teams environment for your organization.

Before you start, check which Microsoft Teams administrator role you have, because access to different cmdlets depends on the role you are assigned in Azure Active Directory (Azure AD).

There are four Microsoft Teams admin roles:

  • Global Administrator or Teams Service Administrator — Can manage and create a Microsoft 365 group and oversee the organization’s Teams service
  • Teams Communications Administrator — Can configure and manage calling and meeting features within Teams
  • Teams Communications Support Engineer — Can troubleshoot communication issues within Teams using advanced tool set, which includes the full list of the Call Analytics features
  • Teams Communications Support Specialist — Can troubleshoot communications issues within Teams with limited access to users’ information (this is the least powerful role)

Getting Started with PowerShell to Manage Microsoft Teams

Install the Teams PowerShell module and Sign in

The very first thing you need to do is install the PowerShell module for Microsoft Teams in your machine and sign then in to your O365 Tenancy prior to executing any PowerShell cmdlet for Teams.

Open Windows PowerShell console as Administrator and enter:

PS> Install-Module -Name MicrosoftTeams

Use this script to log in to Teams using your username and password for the tenant:

$cred=Get-Credential

Connect-MicrosoftTeams -Credential $cred

If multi-factor authentication is enabled for your credentials, log in using the following script and then enter your O365 credentials:

Connect-MicrosoftTeams

Useful Microsoft Teams Powershell Commands

List the available cmdlets.

To get a list of all available cmdlets, use this command:

PS> Get-TeamHelp

Note that PowerShell allows you to create custom cmdlets as well.

The following cmdlets are necessary to start managing Microsoft Teams via PowerShell:

Create a New Team using PowerShell

To create a new team using PowerShell, use:

#Create New TeamNew-Team-DisplayName"Managers Team"-VisibilityPrivate

This PowerShell creates a team. Please note, the New-Team cmdlet takes only two mandatory parameters as given above. You can add optional parameters such as description, owners, etc.

New-Team -DisplayName "Managers Team" -Visibility Public -Description "Teams for Managers" -MailNickName "Teammgr" -Owner "[email protected]"

Update Team Settings using PowerShell

Similarly, You can update the existing team with PowerShell. E.g. Let’s change display name using PowerShell

Set-Team –GroupId "3425c43a-963a-2356-1265-958424234230d7" -DisplayName "Managers Team"

PowerShell to Delete a Team

To remove a Microsoft team with PowerShell, use Remove-Team cmdlet. E.g.

Remove-Team –GroupId "3425c43a-963a-2356-1265-958424234230d7"

Manage Members of the Team via PowerShell 

To get membership of an existing team using: Get-TeamUser cmdlet. This gets you all users of the team.

Get-TeamUser -GroupId "c45423a4353b-44a9-4bcc-8d9a-123b9743c5345

How to get owners and members of the team

#Get Team OwnersGet-TeamUser-GroupId"d06fc37a-44a9-4bff-8d9a-7f57616be6fa"-RoleOwner#Get Team MembersGet-TeamUser-GroupId"d06fc37a-44a9-4bff-8d9a-7f57616be6fa"-RoleMember#Get Team GuestsGet-TeamUser-GroupId"d06fc37a-44a9-4bff-8d9a-7f57616be6fa"-RoleGuest

To add an owner to a Team.

Add-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "[email protected]" -Role Owner

How to add a user as a member of a team

Add-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "[email protected]" -Role Member

How to add a user to all teams?

#Get all Teams $AllTeams= Get-Team$UserToAdd= "[email protected]"#Add a user to all teamsForEach($Teamin$AllTeams){Write-Host"Adding to $($Team.DisplayName)”Add-TeamUser-GroupId$Team.GroupID-User$UserToAdd-RoleMember}

How to remove a user from a Team

#Remove user from teamRemove-TeamUser-GroupId"d06fc37a-44a9-4bff-8d9a-7f57616be6fa"-User"[email protected]"

Managing Microsoft Teams policies

Policies within Microsoft Teams govern over a user’s or team’s abilities within teams and channels. Policies can enforce on behalf of a single user or an entire organization.  The automation that PowerShell provides allow the Microsoft Teams administrator the ability to assign custom policies to multiple users as required.  

In this example, the following script assigns the Human Resources Management Policy to all users in the Human Resources group. The script begins by getting the GroupObjectId of the group.  Once acquired, it then finds the members of that group and assigns the policy to all users in the group.

$group = Get-AzureADGroup -SearchString "Human Resources group"
$members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true | Where-Object {$_.ObjectType -eq "User"}$members | ForEach-Object { Grant-CsTeamsChannelsPolicy -PolicyName "Human Resources Management Policy" -Identity $_.UserPrincipalName}

Leave a Reply

Your email address will not be published. Required fields are marked *