Managing Microsoft Teams using PowerShell

Microsoft Teams is a chat-based collaboration tool that provides efficient collaboration by bringing related tools together. Managing Teams can be challenging, especially for large organizations with multiple teams and channels. This is where PowerShell comes in. PowerShell is a powerful scripting language that can automate many tasks, including managing Teams. In this guide, we will show you how to use PowerShell to manage your Microsoft Teams environment.

With PowerShell, we can automate a lot of stuff as we do with any other Microsoft products such as SharePoint. Starting from administration, customization, governance, standardizing Team settings, etc. There is a set of cmdlets provided for working with the core team and its settings:

  • Managing Teams: New-Team, Get-Team, Set-Team
  • Managing team users Add-TeamUser, Remove-TeamUser
  • Managing the channels of the team New-TeamChannel, Remove-TeamChannel

It’s recommended to run the PowerShell cmdlets as a Global Admin or Teams Service Administrator to work with all teams in your organization. However, end-users can still run these cmdlets, but they’ll work only on the teams they own or are members of.

Microsoft Teams Vs Office 365 Group: Creating a new Microsoft Teams creates an Office 365 group internally (However, creating Office 365 Group does not create Microsoft Teams!). They both share the same ID.

How to use PowerShell to manage Microsoft Teams?

The first thing you need to do is install the PowerShell module for Microsoft Teams in your machine before executing any PowerShell cmdlet for Teams. Open Windows PowerShell console as Administrator and enter:

Install-Module MicrosoftTeams

More on installing the PowerShell module for Microsoft Teams and connecting teams from PowerShell is explained in my other article: How to Connect to Microsoft Teams using PowerShell?

Once you install Teams PowerShell module, you can connect to Teams and work on your requirements. If you have PowerShell ISE open, be sure to restart your PowerShell ISE after installing the module to properly load the cmdlets.

Get a List of Microsoft Teams with PowerShell

To get all teams in your environment, use the following:

#Connect to Microsoft Teams
Connect-MicrosoftTeams -Credential (Get-Credential)

#Get all Teams
Get-Team

This PowerShell lists all teams from your Office 365 environment. This cmdlet will get GroupID, DisplayName, Visibility, Archived, MailNickName, and Description as output.

microsoft teams powershell list all teams

We can audit all teams and generate a report with all the users and their permission levels in teams. Let’s get the list of all teams inventory and export it to a CSV file:

#Connect to Microsoft Teams
Connect-MicrosoftTeams -Credential (Get-Credential)

#Get all Teams
$AllTeams = Get-Team
$TeamData = @()

Foreach ($Team in $AllTeams)
{
    #Collect Team Data
    $TeamObjectID = $Team.GroupId.ToString()
    $TeamData += [PSCustomObject] @{
        TeamName = $Team.DisplayName
        TeamID =  $TeamObjectID
        MailAlias = $Team.MailNickName
        TeamType =  $Team.Visibility
        TeamOwners = (Get-TeamUser -GroupId $TeamObjectID | Where {$_.Role -eq 'Owner'}).Name -join '; '
        TeamMembers = (Get-TeamUser -GroupId $TeamObjectID | Where {$_.Role -eq 'Member'}).Name -join '; '
    }
}
$TeamData | Format-Table
$TeamData | Export-CSV "C:\Temp\TeamsList.csv" -NoTypeInformation 

This PowerShell script generates a list of all teams along with Members and Owners of each team.

Create a New Team using PowerShell

To create a new team using PowerShell, use:

#Create New Team
New-Team -DisplayName "Warehouse Managers" -Visibility Private

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 "Warehouse Managers" -Visibility Public -Description "Teams for Warehouse Managers" -MailNickName "warehousemgr" -Owner "Salaudeen@crescent.com"

Update Team Settings using PowerShell

Similarly, You can update the existing team with PowerShell. For example, let’s change the display name using PowerShell.

Set-Team -GroupId "209c685a-903a-4086-9962-9584566950d7" -DisplayName "Crescent Warehouse Managers" 

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

Remove-Team -GroupId "209c685a-903a-4086-9962-9584566950d7"

Manage Members of the Team via PowerShell 

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

Get-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa"

To get owners and members of the team, use the following:

#Get Team Owners
Get-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -Role Owner

#Get Team Members
Get-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -Role Member

#Get Team Guests
Get-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -Role Guest

To add an owner to a Team with PowerShell, use the Add-TeamUser cmdlet:

Add-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "Steve@crescent.com" -Role Owner

This sets the owner of the team. To add a user as a member of a team, use:

Add-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "Steve@crescentintranet.com" -Role Member

How about adding a user to all teams?

#Get all Teams 
$AllTeams = Get-Team
$UserToAdd = "Steve@Crescent.com"
 
#Add a user to all teams
ForEach ($Team in $AllTeams)
{
    Write-Host "Adding to $($Team.DisplayName)"
    Add-TeamUser -GroupId $Team.GroupID -User $UserToAdd -Role Member
}

To remove a user from Microsoft Teams with PowerShell, use the Remove-TeamUser cmdlet.

#Remove user from team
Remove-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "Steve@crescent.com"

Check out the Microsoft documentation on teams as https://github.com/MicrosoftDocs/office-docs-powershell/tree/master/teams/teams-ps/teams

Wrapping up

In summary, using PowerShell to manage Microsoft Teams can help you automate tasks and streamline your workflow. Whether you’re managing a small business or a large enterprise, PowerShell can help you manage your Teams environment more efficiently. With this guide, you should now have a good understanding of how to use PowerShell to manage your Teams environment, including creating and managing teams, channels, and members.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “Managing Microsoft Teams using PowerShell

  • Your script to generate all teams inventory and export it to a CSV file is excellent. However, how can we customize it to further to read Team Mail Nicknames from a csv file and extract similar info ? If you could please guide. Thanks a lot..!!

    Reply
  • do you know if I can create a word cloud from a MS teams channel to help identify trends?

    Reply

Leave a Reply

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