Skip to the main content.

My top 10 ways to use Teams PowerShell

Table of contents

 

 

Post Featured Image

Here are some very helpful cmdlets (and corresponding explanations) for all Microsoft Teams administrators. How many of Damian's top ten did you already know?


Introduction

Using PowerShell to manage Microsoft Teams can be a boon to administrators in Microsoft 365 and just like Exchange PowerShell, Teams PowerShell has a slew of cmldets available to administrators.  The purpose of this article is to take what is available to us and take a look at some of the top 10 ways to use Microsoft Teams PowerShell. Let's dive right in.

 

ONE: Licensing users

A common onboarding task is to give a user a license to the features that they need access to. For this section we'll briefly go through the process using Microsoft Graph PowerShell and while this process can still be performed with the MSOnline PowerShell module, Microsoft is deprecating this module over the next year.

For a detailed look at this, take a look at a previous blog post on the ScriptRunner blog which can be found here.

 

TWO: Creating teams

Teams are, of course, the key component of the Microsoft Teams experience and as such, knowing how to create them with PowerShell can also be a useful skill especially if you create many teams with similar settings.

Team creation with PowerShell does not require a lot of options, in fact we can create a team using just a Display Name:


New-Team -DisplayName 'Marketing Campaign – Big Corp, Inc.'

The New-Team cmdlet has other options, like providing a description:


New-Team -DisplayName 'Project Rebuild' -Description 'First project of 2024 (HR)'

Or perhaps turning on one feature for a Team like Search:


New-Team -DisplayName 'New Test Group' -ShowInTeamsSearchAndSuggestions $False

When creating Teams, keep in mind that if a feature is not set upon creation, most if not all settings, can be modified in post with the Set-Team cmdlet.

 

THREE: Creating Teams channels

After we've created our Teams, we can then add Teams Channels to subdivide or organize what each Team needs.  For example, we have a Sales Team now and under this Team we need to create channels for each sales drive that the company holds.  A consulting firm may have a Team per client and then a Teams Channel for each project they have for that client in order to segregate communications and sharing of content.

Sample Cmdlets


New-TeamChannel -GroupId 933fe926-555a-4832-87d1-8f700736e003 -DisplayName 'Project 007'
New-TeamChannel -GroupId cfdba387-1319-4f6b-a883-8700046f07e7 -DisplayName 'Internal Sales'

Note that we need the Group ID in  order to create the Teams Channel in that Team.

 

FOUR: Change Teams settings post creation

As alluded to before, Teams that we’ve created previously can also be modified to either meet new needs or possibly create a standard configuration.  These changes can be made with the Set-Team cmdlet.  For example, if we want to change options that Team members have available to them in a Chanel, like Stickers and Memes or mentions:


Set-Team -GroupId d36f235f-d30e-4460-98a2-5728b906fbfd -GiphyContentRating Strict -AllowStickersAndMemes $False
Get-Team -DisplayName 'Marketing' | Set-Team -AllowTeamMentions $False

Or perhaps we want to allow Channel updates:


Set-Team -GroupId cfdba387-1319-4f6b-a883-8700046f07e7 -AllowCreateUpdateChannels $True
Get-Team -DisplayName 'HR Department' | Set-Team -AllowGuestCreateUpdateChannels $False

Turn off Group Search:


Set-Team -GroupId cfdba387-1319-4f6b-a883-8700046f07e7 -ShowInTeamsSearchAndSuggestions $False

Change some group information like Display Name and Description:


Set-TeamChannel -GroupId cfdba387-1319-4f6b-a883-8700046f07e7 -CurrentDisplayName 'Internal Sales' -Description 'Sales Channel'

Or archive a group:


Set-TeamArchivedState -GroupId 9ee34e55-60d6-49cf-8a64-1bc1454d7ee4 -Archived:$True

 

FIVE: Setting policies

In addition to the Teams and Channels, PowerShell can manage the global policies that affect all Teams, Users or perhaps meeting settings.  By modifying these settings we can control the usage of Teams in an environment and perhaps place Teams into compliance with various corporate policies.

For example we can turn on Transcriptions perhaps the RnD department so that they can take advantage of this feature for their meetings, but have it disabled in other policies:


New-CsTeamsMeetingPolicy -Identity RnDPolicy -AllowTranscription $True

Then we have settings for user admission or breakout room creations:


New-CsTeamsMeetingPolicy -Identity 'MarketingPolicy' -AutoAdmittedUsers 'Everyone' -AllowBreakoutRooms $False

Once a policy is listed and available, we can apply the policy to users:


Grant-CsTeamsMeetingPolicy -Identity 'Jsmith' -PolicyName 'ITDeptMeetings'

Or if policy settings become defunct or are no longer needed, we can remove them:


Remove-CsTeamsMeetingPolicy 'RnDPolicy'

And just like Teams, we can modify a policy once it is created and change settings that may meet some new standard:


Set-CsTeamsMeetingPolicy -Identity 'ITPolicy' -AllowBreakoutRooms $False
Set-CsTeamsMeetingPolicy -Identity 'SalesPolicy' -AllowMeetNow $True

 

SIX: Adding members to Teams

What would Teams be without users?  Well, not much actually.  So with PowerShell, we can also add users to Microsoft Teams to make sure users have access to the Teams they need in their clients.  With the below sample code, we can use a source CSV file and then add members to Teams based on their department:


# Add Teams members
$CSV = Import-CSV 'TeamsUsers.csv'
Foreach ($MsolUser in $CSV) {
$Department = $MsolUser.Department
$Email = $MsolUser.PrimarySMTPAddress
If ($Department -eq 'Marketing') {Add-TeamUser -GroupId $MarketingTeam.GroupId -User $Email}
If ($Department -eq 'Sales') {Add-TeamUser -GroupId $SalesTeam.GroupId -User $Email}
If ($Department -eq 'IT') {Add-TeamUser -GroupId $ITTeam.GroupId -User $Email}
If ($Department -eq 'Management') {Add-TeamUser -GroupId $ManagementTeam.GroupId -User $Email}
}

 

SEVEN: Teams role assignment

Assigning roles to those who need to support Microsoft Teams is another important task as we can make sure that the least privilege is used and that these users can perform their tasks.  It is important to note that while these role assignments could be done with the MSOnline PowerShell module, this module is being deprecated and should not be used for production work.  In the below examples, make sure to connect to Microsoft Graph PowerShell first, with the appropriate permissions.

First, we can list roles for Microsoft Teams:


Get-MgDirectoryRoleTemplate | 
	Where-Object DisplayName -like Teams*

Next, we have a role addition sample where we add a member to a role, like Teams Communications Support Engineer which first requires identifying the role:


Get-MgDirectoryRole | 
	Where-Object DisplayName -eq 'Teams Communications Support Engineer'

Then we retrieve the Role’s Id value:


$RoleID = (Get-MgDirectoryRole | 
	Where-Object DisplayName -eq 'Teams Communications Support Engineer').Id

We then store User data in a variable:


$UserId = (Get-MgUser -UserId damian@practicalPowerShell.com).Id

Finally adding the user to Role group:


New-MgDirectoryRoleMemberByRef -DirectoryRoleId $TeamsAdminRoleId -BodyParameter @{"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$($UserId)"}

 

EIGHT: Holiday settings

Holidays are important in Teams because they provide for different functionality during certain dates or times of the year. When a Holiday is created and assigned, Teams can provide alternate messages or even routing for callers.  A common use is to put them in place when the company is closed for business.  Let’s walk through some sample code for handling Holidays in Teams:

List current Holidays:


Get-CsOnlineSchedule
Get-CsOnlineSchedule | Where-Object {$_.Type -eq 'Fixed'}

Create new Holiday:


$Date = New-CsOnlineDateTimeRange -Start '1/12/2020 0:00' -End '1/12/2020 23:45'
New-CsOnlineSchedule -Name 'Employee Day' -DateTimeRanges $Date -FixedSchedule

Change an existing Holiday:


$Schedule = Get-CsOnlineSchedule 93ce710a-f0b6-4c77-80eb-45c73228aa8b
$Schedule.Name = 'New Years Party'
Set-CsOnlineSchedule -Instance $Schedule

Remove existing Holiday:


Remove-CsOnlineSchedule -Id 15f9c478-89a3-4052-9b1b-50eb87e769cf

 

NINE: Log searches

Auditing IT operations is an important task and can be a necessary task for regulations, audits and other compliance related matters. PowerShell excels at this type of task where potentially large sets of data are involved and filtering is needed.  Thus auditing Microsoft Teams events if one of the top ten for Teams PowerShell. In order to audit Teams events, we need to connect to Exchange Online PowerShell (v3) as this is where the Unified Audit log resides.

Teams Events  stored in the Unified Audit Log:


# Past 48 hours
$EndDate = Get-Date
$StartDate = $EndDate.AddDays(-2)

All Teams events:


Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType MicrosoftTeams

Teams channels added:


Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType MicrosoftTeams -Operations ChannelAdded

Sensitivity Label changes:


Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType MicrosoftTeams -Operations SensitivityLabelChanged

Teams are deleted:


Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType MicrosoftTeams -Operations TeamDeleted

For Reference  Teams activities

 

TEN: Teams cleanup

While creating and managing Teams are important tasks, organizations also tend to create too many Teams and in other instances organizations work hard to clear excess Teams on a periodic basis.  As such, Teams cleanup is an important task and can be completed with the Remove-Team* set of cmdlets. Below are some sample usages of these cmdlets:

Remove Existing Microsoft Team:


Remove-Team -GroupID '933fe926-555a-4832-87d1-8f700736e003'

Remove Existing Microsoft Team Channel:


Remove-TeamChannel -GroupId cfdba387-1319-4f6b-a883-8700046f07e7 -DisplayName 'Internal Sales'

Remove Existing Microsoft Team User:


Remove-TeamUser -GroupID '933fe926-555a-4832-87d1-8f700736e003' -User <UPN> 

 

Conclusion

As we can see from the Top Ten ways to use PowerShell for Microsoft Teams there are quite a few tasks that we can automate or make easier. From creating Teams, Changing Teams settings to logging and more, PowerShell provides administrators with a programmatic way to manage their environment.  With time and practice, the above cmdlets and chores could be automated which would then allow administrators to pass tasks off to other teammates or perhaps do some other projects that have been waiting for a while.  The key takeaway is that Microsoft has vested a lot of time and energy into PowerShell and this top ten only scratches the proverbial surface with much more to learn.

 

 

 

 

Good2know

Webinar: PowerShell magic for Microsoft Teams admins made easy!

Managing Teams can be pretty time consuming. Have you ever wondered how you could optimize and reduce the arising workload?

The PowerShell module for Microsoft Teams is your key: it allows you to standardize and automate many repetitive tasks.

For example:

  • Finding teams with no owners
  • Bulk creation/deletion/archiving of teams
  • Adding/removing channels/users in all/selected teams
  • Managing security settings in all/selected teams
  • Creating reports of your current Teams infrastructure
  • Pushing messages to Teams channels

This webinar is aimed at administrators, IT and DevOps professionals, PowerShell developers and IT managers.

 scriptrunner-microsoft-teams-webinar

This is what we showed in the webinar:

 

  • The options of the current Teams PowerShell module
  • How ScriptRunner allows you to save time by delegating tasks to helpdesk teams and even end-users
  • Take a look at our ready-to-use PowerShell scripts
  • The centralized management of all PowerShell components like scripts, modules, credentials
  • How you can automatically turn every script into an easy-to-use web form

 

Click here for the webinar recording!

 

 

 

Related links

Related posts

4 min read

PowerShell benefits for SharePoint

10 min read

My top 10 ways to use Teams PowerShell

12 min read

Licensing with Microsoft Graph PowerShell

About the author: