Category filter

Script to connect to Microsoft Teams with PowerShell

This document walks you through the process of executing a script to connect to Microsoft Teams with PowerShell on Windows devices.

Microsoft Teams is a collaboration platform developed by Microsoft, designed to facilitate communication and teamwork within organizations.

Connecting to Microsoft Teams via PowerShell empowers IT admins to utilize PowerShell commands for centralized management of Teams, including tasks such as user provisioning, channel management, team creation, meetings management, seamless integration with other Microsoft services and others. This can be achieved by using the script provided below, which can be seamlessly deployed using Hexnode’s Execute Custom Script action.


The sample script provided below are adapted from third-party open source sites.

Creating a script to connect to Microsoft Teams via PowerShell

The script works in the following manner. Initially, the script installs the MicrosoftTeams module using the Install-Module cmdlet with the -Force parameter, enforcing the installation even if a newer version is already present.

Following this, the script proceeds to define the username and password required for authentication. To ensure security, it converts the password string into a secure format before further processing. With this information, it creates a PSCredential object, encapsulating the username and secure password. A PSCredential object in PowerShell securely stores authentication credentials, such as usernames and passwords, for authenticating with services or resources.

Following the import of the MicrosoftTeams module, the script uses the Connect-MicrosoftTeams cmdlet to establish a connection to Microsoft Teams, utilizing the provided credentials for authentication. It then checks the status of the connection to Microsoft Teams and displays a message in the Action History of the Hexnode UEM portal indicating whether the authentication process was successful or not.

Verify the status after executing a script to connect to Microsoft Teams with PowerShell

Microsoft Teams PowerShell cmdlets are a set of commands that can be used to manage Microsoft Teams using PowerShell, such as creating and managing teams, channels, and users. To execute these cmdlets from the Hexnode UEM console, you can use the script provided below.


This script initiates by establishing a connection with Microsoft Teams. It then proceeds to execute the specified Microsoft Teams PowerShell cmdlets, in this instance, the Get-Team cmdlet. The Get-Team cmdlet is utilized to retrieve information about all the teams that a specific user is a member of, or all teams within the organization. Similarly, other Microsoft Teams PowerShell cmdlets can be executed by replacing the desired cmdlets in place of the Get-Team cmdlet.

In this script, the Out-Null cmdlet is utilized to suppress the output of the Connect-MicrosoftTeams -Credential $credential command from appearing on the console. This is because the output of the Connect-MicrosoftTeams command often takes precedence, and any other output may be suppressed.

Details of all the existing teams in Microsoft Teams will be displayed

PowerShell script to disconnect from Microsoft Teams

This command will terminate the current session with Microsoft Teams, effectively ending the connection.


  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository