Skip to main content

Microsoft

Create and Manage Microsoft Teams and Channels with PowerShell

Analyst Working With Computer In Business Analytics And Data Management System With Kpi And Metrics Connected To The Database For Technology Finance, Operations, Sales, Marketing. Data Analysis.ai

In this blog, we will walk through the process of creating a Team, adding Team Channels, and managing owners and members within Microsoft Teams using PowerShell scripts. You’ll learn how to automate the creation of Teams, organize them with different channels, and efficiently manage user roles and permissions, such as assigning ownership or membership. This will help you streamline administrative tasks in Microsoft Teams, enabling you to maintain better control and organization within your Teams environment.

Prerequisites for Creating and Managing Microsoft Teams with PowerShell

For executing certain commands, you should have the Microsoft Teams PowerShell module installed on your computer. Use the PowerShell cmdlet below to install the Microsoft Teams PowerShell module. I am performing this in PowerShell ISE.

Install-Module -Name MicrosoftTeams -Force -AllowClobber

Once the setup is completed, open the PowerShell ISE in administrator mode and set up a new team.

Create a Team with PowerShell

To work with Teams using PowerShell, connect the Teams module by importing it in code as follows:

# Sign into Teams online
Import-Module -Name MicrosoftTeams
$credential = Get-Credential
Connect-MicrosoftTeams -Credential $credential

You will be prompted to enter your credentials when you execute the above commands. Ensure that you provide the admin credentials for Microsoft Teams.

Next, create the team as either Private or Public using the following command:

New-Team -DisplayName <TeamName> -Visibility <Private/Public> -Description <DescriptionAboutTeam> -MailNickName <AliasName> -Owner <TeamOwnerEmail>

Ex: New-Team -DisplayName  “Test Team” -Description “This Team is for testing purpose” -Visibility Private -MailNickName TestTeam -Owner abc@xyz.com

Note: Changes made to Teams via PowerShell may take up to two hours to take effect and appear in both the Office 365 admin portal and the Teams client.

Once the team is created, it will display the information about the newly created Team as follows:

Newteam

Parameters

  • Visibility: Public Teams enable any user to join the group, while Private Teams require approval from an owner before a user can join.
  • MailNickName: Email alias for the associated Office 365 group. MailNickName will be used as the PrimarySmtpAddress.
  • Owner: The Team owner can add or remove people from the Team. If the owner is not specified, it is set to the admin user who created the Team.

Add Users to a Team

To add users, you need the GroupId. Use the PowerShell command below to retrieve the GroupId based on the Teams display name.

# Get Team ID
$Group = Get-Team -DisplayName <TeamDisplayName> | select GroupId

This command will return the GroupId, display name, visibility, description, and other details. Then, add the users to the group by executing the following cmdlet.

# Add user to Team
Add-TeamUser -GroupID $Group.GroupId -User username@xyz.com

By default, it adds the user as a Member of the Team. To add a user as the owner of a team, include the -Role parameter and set its value for the owner.

Remove Users from a Team

# Get Team ID
$Group = Get-Team -DisplayName <TeamDisplayName> | select GroupId
# Remove user from Team
Remove-TeamUser -GroupID $Group.GroupId -User abc@xyz.cloud

Once a user is removed from a team, they can no longer access the team’s channels, files, or conversations.

Note: Only the team owner has the authority to execute this command and remove a user

Create Channels for a Team

After the team is created, you can add a channel to it. To do this, use the New-TeamChannel cmdlet with the following syntax.

New-TeamChannel -GroupId $GroupId -DisplayName <NewChannelDisplayName> -MembershipType <Private/Shared/Standard> -Owner <UserEmail>

Parameters

  • MembershipType: Channel membership type, Standard, Shared, or Private.
  • DisplayName: Name of the channel that needs to be created
  • Owner: UPN of owner that can be specified while creating a private channel.

Note: A SharePoint site is also generated when a private or shared channel is created in a team. Access to this site is restricted to the owners and members of the respective private or shared channel.

A team site is automatically created in SharePoint whenever a new team or a private/shared channel is created in Microsoft Teams. The site’s description or classification can be edited within the channel settings in Teams.

Add/Remove Users to a Team Channel

We can manage users within a specific channel, enabling us to assign them as either channel owners or members.

The command will return immediately, but the Teams application will not reflect the update immediately. To see the update, you should refresh the members page.

# Get the Group ID of a Team
$GroupId = (Get-Team -DisplayName <TeamDisplayName>).GroupId
#Add user to the Channel in Teams
Add-TeamChannelUser -GroupId $GroupId -DisplayName <ChannelDisplayName> -User <UserEmail>

You can include a role parameter if the user needs to be the channel owner. Refresh the members page to view the changes in the Team application.

Note: Due to technical limitations of private channels, users must first be members of the team to be added as members of a channel. Additionally, to become an owner of a channel, someone must first be a member of that channel.

Similarly, the following command can be used to remove a user from the team channel:

# Get the Group ID of a Team
$GroupId = (Get-Team -DisplayName <TeamDisplayName>).GroupId
#Remove user from the Channel in Teams
Remove-TeamChannelUser -GroupId $GroupId  -DisplayName <ChannelDisplayName> -User <UserEmail>

Remove a Channel from a Team

The following cmdlet deletes a specific channel from a Microsoft Teams team using the PowerShell module.

# Get the Group ID of a Team 
$GroupId = (Get-Team -DisplayName <TeamDisplayName>).GroupId
Remove-TeamChannel -GroupId $GroupId -DisplayName <ChannelDisplayName>

This action will not delete any content from the tabs associated with the channel. Also, only team admins or users with specific permissions are authorized to delete a channel from the team.

Note: This will “soft delete” a channel, which means its contents are not permanently removed immediately. You can restore this within 21 days before it gets permanently deleted. So, any new request to create a channel with the same information will fail for 21 days.

Deleting a standard channel does not delete the channel site. The channel is removed, but the files remain. Deleting a private channel removes the entire site, including all files and conversations.

Finally, disconnect the Microsoft Teams environment from the PowerShell using the following command:

Disconnect-MicrosoftTeams

Conclusion

You can efficiently create and manage Microsoft Teams and channels by utilizing the commands provided, allowing for seamless administration. This also offers the flexibility to manage users within both the teams and individual channels, whether private or shared. This gives you complete control over user roles, permissions, and access.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pallavi Koramkar

I am working as a Lead Technical Consultant in Perficient, Nagpur. Currently working on Microsoft web-based collaborative platforms as a senior developer. I have worked on a broader range of Microsoft technologies like .NET framework, MS SQL, Azure, ASP.NET and web forms, SharePoint framework, etc. Till now, I have provided different technical solutions to clients to solve their business problems. I enjoy taking on new challenges and completing them efficiently.

More from this Author

Follow Us