Managing Microsoft Teams using PowerShell

Microsoft Teams is a chat-based collaboration tool that provides efficient collaboration by bringing related tools together. With PowerShell, we can automate a lot much stuff as we do with any other Microsoft products such as SharePoint. Starting from administration, customization, governance, standardizing Team settings, etc. There are 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 running the PowerShell cmdlets as a Global Admin or Teams Service Administrator to be able to work with all teams in your organization. However, end-user can still run these cmdlets but they'll work only on the teams they own or are a member 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 very first thing you need to do is install the PowerShell module for Microsoft Teams in your machine prior to executing any PowerShell cmdlet for Teams. Open Windows PowerShell console as Administrator and enter:
Install-Module MicrosoftTeams
More on installing 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 working on your requirements. If you have PowerShell ISE open, be sure to restart your PowerShell ISE after installing the module, so that it properly loads the cmdlets.

Get a List of Microsoft Teams with PowerShell
To get all teams in your environment, use:
#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 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 "[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 "209c685a-903a-4086-9962-9584566950d7" -DisplayName "Crescent Warehouse Managers" 

PowerShell to Delete a Team
To remove a Microsoft team with PowerShell, use Remove-Team cmdlet. E.g.
Remove-Team –GroupId "209c685a-903a-4086-9962-9584566950d7"

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 "d06fc37a-44a9-4bff-8d9a-7f57616be6fa"
To get owners and members of the team, use:
#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 Add-TeamUser cmdlet:
Add-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "[email protected]" -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 "[email protected]" -Role Member

How about adding a user to all teams?
#Get all Teams 
$AllTeams = Get-Team
$UserToAdd = "[email protected]"
 
#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 Remove-TeamUser cmdlet.
#Remove user from team
Remove-TeamUser -GroupId "d06fc37a-44a9-4bff-8d9a-7f57616be6fa" -User "[email protected]"

Checkout the Microsoft documentation on teams as https://github.com/MicrosoftDocs/office-docs-powershell/tree/master/teams/teams-ps/teams
Managing Microsoft Teams using PowerShell Managing Microsoft Teams using PowerShell Reviewed by Salaudeen Rajack on July 10, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.