Skip to content

/errors/teams/must-call-connect-microsoftteams

You must call the Connect-MicrosoftTeams cmdlet before calling any other cmdlets.

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Microsoft Teams PowerShell
exit code
1
versions
MicrosoftTeams 8.0.0
os
windows, linux, macos

You must call the Connect-MicrosoftTeams cmdlet before calling any other cmdlets. is the message of the team management cmdlets in the MicrosoftTeams module (Get-Team, New-Team, Add-TeamUser, Get-TeamChannel, ...) when there is no Teams session. Connect, then run the command again:

powershell
Connect-MicrosoftTeams
Get-Team -DisplayName "Marketing"

Reproduce it

MicrosoftTeams 8.0.0 imported, no connection:

powershell
Import-Module MicrosoftTeams
try {
  Get-Team -DisplayName "Marketing" -ErrorAction Stop
} catch {
  $_.Exception.Message
  $_.Exception.GetType().FullName
}

Output

You must call the Connect-MicrosoftTeams cmdlet before calling any other cmdlets.
System.UnauthorizedAccessException

The exception type is System.UnauthorizedAccessException, so catch [System.UnauthorizedAccessException] catches it in scripts.

Two messages, one cause

The module contains two families of cmdlets with different not-connected messages:

CmdletsMessage without a session
Get-Team, New-Team, Add-TeamUser, Get-TeamChannelYou must call the Connect-MicrosoftTeams cmdlet before calling any other cmdlets.
Get-CsOnlineUser, Grant-Cs*Policy, Set-Cs*, New-CsBatchPolicyAssignmentOperationSession is not established, run Connect-MicrosoftTeams before requesting access token

Both are fixed by Connect-MicrosoftTeams. If the error persists right after connecting, check that the script did not open a new runspace (Start-Job, ForEach-Object -Parallel) where the session does not exist.

What the message means

The MicrosoftTeams module keeps the signed-in session in memory for the current PowerShell runspace. Every team cmdlet checks for that session before it sends anything; when none exists it throws this message as a System.UnauthorizedAccessException. The command did not reach Microsoft Teams, so nothing was read or changed. The wording "before calling any other cmdlets" is generic: importing the module and running Get-Help or Get-Command still work without a session.

How to recognise each cause

CauseClue
Never connectedFirst Teams command in a new window or at the top of a script that has no Connect-MicrosoftTeams line
Session endedCommands worked earlier; Disconnect-MicrosoftTeams ran, or a profile or script opened a new PowerShell process
Different runspaceWorks interactively, fails inside Start-Job, ForEach-Object -Parallel or a runspace pool
Connection failed silentlyConnect-MicrosoftTeams was wrapped in try/catch or -ErrorAction SilentlyContinue and its sign-in error was swallowed

Fix and confirm

  1. Run Connect-MicrosoftTeams. Interactive sign-in opens a browser; on a server without one, add -UseDeviceAuthentication.
  2. Watch the output: a successful connection prints the account, environment and tenant ID. An error at this step (for example, conditional access or MFA) is the real problem to solve.
  3. Rerun the command, for example Get-Team -User you@contoso.com. Results, or an empty result without an error, confirm the session.

Handle it in scripts

Because the exception type is fixed, a script can connect only when needed:

powershell
try {
  $null = Get-Team -User admin@contoso.com -ErrorAction Stop
} catch [System.UnauthorizedAccessException] {
  Connect-MicrosoftTeams
}

For scheduled jobs, connect with a certificate at the start of the script and avoid moving Teams work into separate jobs. The Cs cmdlets have their own wording for the same situation: Session is not established. All sign-in options are on Connect-MicrosoftTeams.

FAQ

Does this error mean my account lacks permissions?

No. The cmdlet stops before contacting the service because the module has no session. Permission problems appear only after a successful Connect-MicrosoftTeams, as errors returned by the service.

Do I need to connect again in every new PowerShell window?

Yes. The session lives in the PowerShell process and runspace that ran Connect-MicrosoftTeams. A new window, a background job or a parallel block starts without it.

How do I connect without a browser on a server?

Use Connect-MicrosoftTeams -UseDeviceAuthentication to sign in from another device, or certificate-based app-only sign-in with -CertificateThumbprint, -ApplicationId and -TenantId for unattended scripts.

Sources