How to Connect to Microsoft Teams with PowerShell?

Requirement: Connect to Microsoft Teams using PowerShell.

Microsoft Teams is a platform for team collaboration in the workplace, and it can be used for real-time communication, sharing files, managing projects, and more. As Microsoft Teams is a crucial part of the communications strategy in most organizations, the combination of PowerShell provides a powerful way to automate tasks. In this post, I’ll walk you through how to connect to Microsoft Teams using PowerShell. We’ll also see some basic commands you can use once connected. Let’s get started!

Connect to Microsoft Teams using PowerShell

Connecting to Microsoft Teams using PowerShell is simple and straightforward. To connect to Microsoft Teams using PowerShell, you will need to first install the Microsoft Teams PowerShell module. This module can be installed from the PowerShell Gallery by running the following command: Install-Module -Name MicrosoftTeams. Once the module has been installed, you will need to connect to your Microsoft Teams account by running the Connect-MicrosoftTeams cmdlet. This cmdlet will prompt you for your Microsoft Teams credentials, and once you have entered them, you will be connected to your Microsoft Teams account. From there, you can use all the cmdlets in the MicrosoftTeams module to manage your Microsoft Teams environment.

Step 1: Install Microsoft Teams PowerShell Module

To start with, you need to install the PowerShell module for Microsoft Teams. Check if you have the Teams module installed already by: 

Get-module *teams* 

This gets you the installed Teams modules along with their versions.

check teams powershell module is instaled

This gets you the Microsoft Teams PowerShell version.

How do I get the Microsoft Teams PowerShell module? If the Teams module is not installed already, here is how to install it.

  1. Open Windows PowerShell with “Run as Administrator”, and Enter
    1
    Install-Module -Name MicrosoftTeams
  2. Confirm the prompt by entering “A” Yes to All. The module will start to download and install.

In case You want to use it along with the previous versions of the Microsoft Teams module, use the “AllowClobber” switch.

install-module -name microsoftteams -force -allowclobber

Step 2: Connect to Microsoft Teams using PowerShell

Once you have the Teams PowerShell module installed, you can connect to Teams using 

Connect-MicrosoftTeams

You’ll get a popup to enter your credentials. This Popup is multi-factor authentication (MFA) aware! You can connect Microsoft Teams PowerShell with MFA by leaving the “-Credential” parameter.

connect to microsoft teams with powershell

You can also use the credential parameter to get your organizational account or Microsoft ID credentials.

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

Can I connect to Microsoft Teams with a hard-coded user name and password through PowerShell? Sure! Here is the PowerShell script to connect with the username and password in the script.

#Import the Microsoft Teams module
Import-Module -Name MicrosoftTeams

#Parameters for Credentials
$UserName = "Salaudeen@crescent.com"
$PassWord = ConvertTo-SecureString -String "Password goes here" -AsPlainText -Force

#Create a Credential object
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName, $PassWord

#Connect to Microsoft Teams with given user name and password
Connect-MicrosoftTeams -Credential $Cred

#Get All Public Teams
Get-Team | Where {$_.Visibility -eq "Public"}

Make sure you have either a global administrator or Teams service administrator rights before connecting to Teams. Although any team owner or member can connect to Teams with PowerShell, their permissions are limited within the teams.

Step 3: Start using Cmdlets for Microsoft Teams

Once you have established connectivity with Microsoft Teams, you can start using PowerShell cmdlets. For example, let’s use PowerShell to get a list of Microsoft Teams.

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

#Get all Teams
Get-Team

Disconnect Microsoft Teams from PowerShell

To disconnect the Microsoft Teams environment from PowerShell, run the cmdlet:

Disconnect-MicrosoftTeams

How do I update the Microsoft Teams PowerShell module?

To upgrade the Teams module, run:

Update-Module MicrosoftTeams

How to uninstall Microsoft Teams PowerShell?

For some reason, If you want to uninstall the Microsoft Teams PowerShell module, use:

UnInstall-Module MicrosoftTeams

Connect to Microsoft Teams PowerShell Module using a Certificate

To connect to the Microsoft Teams PowerShell module with a certificate, create an Application ID through App registration in Microsoft Entra (Azure AD Portal), grant the following Microsoft Graph API Application Permissions, and grant admin consent. Here’s a brief guide:

1. Create an App ID in Microsoft Entra

  1. Go to the Microsoft Entra admin center (https://entra.microsoft.com/)
  2. Navigate to the “App registrations” section
  3. Click on “New Registration” to create a new App ID
  4. Provide a name for your application and select the supported account types
  5. Click “Register” to create the App ID

2. Add a self-signed certificate:

  1. Open the newly created App ID
  2. Go to the “Certificates & secrets” section
  3. Click on “Upload certificate” and select the self-signed certificate file
  4. Click “Add” to upload the certificate to the App ID

3. Grant Microsoft Graph Application permissions:

  1. Still, within the App ID, go to the “API permissions” section
  2. Click on “Add permission” and select “Microsoft Graph”
  3. Choose “Application permissions” to grant permissions to the app itself
  4. Select the permissions below for your application
  5. Click “Add permissions” to add the selected permissions to your App ID
  • User.Read.All
  • Group.ReadWrite.All
  • AppCatalog.ReadWrite.All
  • TeamSettings.ReadWrite.All
  • Channel.Delete.All
  • ChannelSettings.ReadWrite.All
  • ChannelMember.ReadWrite.All
  1. After adding the permissions, you’ll see a “Grant admin consent” button
  2. Click on the button to grant admin consent for the selected permissions
  3. Confirm the action by clicking “Yes” in the confirmation prompt

For more on creating an application ID, see How to Create an App ID in Microsoft Entra.

Refer to the Microsoft documentation for further info on MS Teams resources: Application-based authentication in Teams PowerShell Module

Add Teams Administrator Role to the App ID

Furthermore, you must add the application ID to the “Teams Administrator” role in Microsoft Entra. To add the Teams Administrator role to an App ID in Microsoft Entra, you need to follow these steps:

  1. Open the Microsoft Entra admin center (https://entra.microsoft.com/).
  2. Navigate to the “Roles and Administrators” section >> Search for the “Teams Administrator” role.
  3. Click on the “Teams Administrator” role to open its details page.
  4. In the “Teams Administrator” role details page, Click on the “Add assignments” button.
  5. Search for the App ID to which you want to assign the Teams Administrator role >> Select your App ID.
add Teams Administrator

Here is the PowerShell script to connect to Microsoft Teams PowerShell module using a certificate:

#Set parameters
$AppId = "3735f461-fdb5-4360-8184-b30345e57796" #Your-App-Client-ID
$TenantId = "c1c2b173-4240-4775-a49a-6351b30cd254" #Your Tenant ID
$CertificateThumbprint = "63FDDD807DE01C0EB34CE5647D086D3C153C0FA7" #Your-Certificate-Thumbprint

#Connect to Microsoft Teams using certificate based authentication
Connect-MicrosoftTeams -CertificateThumbprint $CertificateThumbprint -ApplicationId $AppId -TenantId $TenantId

#Get All Teams
Get-Team

Alternatively, You can connect using the certificate attached to the App ID. I assume the “Graph API Cert” is attached to your App ID in the Microsoft Azure Active Directory (Microsoft Entra).

#Set parameters
$AppId = "3735f461-fdb5-4360-8184-b30345e57796" #Your-App-Client-ID
$TenantId = "c1c2b173-4240-4775-a49a-6351b30cd254" #Your Tenant ID
$Password = "Password1"  #PFX Certificate Password

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\Documents\Graph API Cert.pfx",$password)

Connect-MicrosoftTeams -Certificate $cert -ApplicationId $AppId -TenantId $TenantId

Get All PowerShell Cmdlets available in the Teams Module

To get all available cmdlets for Teams, you can use the PowerShell command:

Get-Command -Module MicrosoftTeams
NameDescription
Add-TeamUserThe ‘Add-TeamUser’ adds an owner or member to the team, and to the unified group which backs the team.
Connect-MicrosoftTeamsThe Connect-MicrosoftTeams cmdlet connects an authenticated account to use for Microsoft Teams cmdlet requests. You can use this authenticated account only with Microsoft Teams cmdlets.
Disconnect-MicrosoftTeamsDisconnects from the Microsoft Teams environment
Get-CsPolicyPackageThis cmdlet supports retrieving all the policy packages available on a tenant.
Get-CsUserPolicyPackageThis cmdlet supports retrieving the policy package that’s assigned to a user.
Get-CsUserPolicyPackageRecommendationThis cmdlet supports retrieving recommendations for which policy packages are best suited for a given user.
Get-TeamThis cmdlet supports retrieving teams with particular properties/information, including all teams that a specific user belongs to, all teams that have been archived, all teams with a specific display name, or all teams in the organization.
Get-TeamChannelGet all the channels for a team.
Get-TeamsAppReturns app information from the Teams tenant app store.
Get-TeamUserReturns users of a team.
Grant-CsUserPolicyPackageThis cmdlet supports applying a policy package to users in a tenant. Note that there is a limit of 20 users you can apply the package to at a time.
New-CsBatchPolicyPackageAssignmentOperationThis cmdlet submits an operation that applies a policy package to a batch of users in a tenant. A batch may contain up to 5000 users.
New-TeamThis cmdlet lets you provision a new Team for use in Microsoft Teams and will create an O365 Unified Group to back the team.
New-TeamChannelAdd a new channel to a team.
New-TeamsAppCreates a new app in the Teams tenant app store.
Remove-TeamThis cmdlet deletes a specified Team from Microsoft Teams.
Remove-TeamChannelDelete a channel. This will not delete content in associated tabs.
Remove-TeamsAppRemoves an app in the Teams tenant app store.
Remove-TeamUserRemove an owner or member from a team, and from the unified group which backs the team.
Set-TeamThis cmdlet allows you to update properties of a team, including its displayname, description, and team-specific settings.
Set-TeamArchivedStateThis cmdlet is used to freeze all of the team activity, but Teams Administrators and team owners will still be able to add or remove members and update roles
Set-TeamChannelUpdate Team channels settings.
Set-TeamsAppUpdates an app in the Teams tenant app store.

Conclusion

Although the Microsoft 365 admin center can be used to administer various Microsoft 365 services like Azure AD, Teams, SharePoint Online, and Exchange Online, PowerShell provides more flexibility and additional capabilities that help Admins save a lot of time and effort. By following the steps outlined in this guide, you should now be able to connect to Microsoft Teams and use the available cmdlets to manage teams and channels, retrieve user and team information, and perform other tasks. This can help you to improve the management of Microsoft Teams in your organization, as well as automate routine tasks and gather insights on the usage of Microsoft Teams.

You can automate a lot of many tasks in your Microsoft Teams. Here is my related post on managing Microsoft Teams using PowerShell: How to Manage Microsoft Teams using PowerShell?

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!

7 thoughts on “How to Connect to Microsoft Teams with PowerShell?

  • Hi!
    How can I send a message to a person using this?

    Reply
  • Doing so, you can’t run an automated script.
    I can do this in AzureAd – Connect-AzureAD -AccountId $UPNAccount
    But in Teams this fails –
    Connect-MicrosoftTeams -Credential $UPNAccount
    Connect-MicrosoftTeams : Cannot bind parameter ‘Credential’. Cannot convert the “upn-account” value of type “System.String” to type “System.Management.Automation.PSCredential”.

    Reply
  • Do we have a way of using the Connect-MicrosoftTeams with MFA ? if so someone want to share some info?

    Reply
    • Just use “Connect-MicrosoftTeams” without -Credential parameter. You’ll get a login popup – which is MFA aware!

      Reply
      • Thanks, That worked!

        Reply
      • Yes, with MFA environment, Just a simple command Connect-MicrosoftTeams and should bring up the login screen then you have to authenticate via app or text, whatever you’re setup in MFA setting. You should be good.

        Reply

Leave a Reply

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