Connect To Teams (MS Graph)

Review the MicrosoftTeams connection commands and the recovered Connect-XTeams helper. The helper checks for an existing connection; it has not been executed or accepted here.

This is the quick way to connect to MicrosoftTeams using powershell

Connect-MicrosoftTeams

You can disconnect from Teams with

Disconnect-MicrosoftTeams

Alternatively you can copy this function in to your own custom module in powershell.

The source uses Connect-XTeams as its custom connection helper.

This function checks to see if there is an existing connection to MS Teams before creating a new one.

<#

.SYNOPSIS

Connect to Teams in M365.

.DESCRIPTION

Connect to Teams in M365.

It checks to see if there is an active connection.

If there isn't then a new connection is made.

#>

function Connect-XTeams {

param(

[Boolean] $showExamples = $false

)

$alreadyConnectedToTeams = $true
$connectionAttemptFailed = $false

try {

$null = Get-CsTenant

}catch{

Try {

Write-Host "Connecting To MS Teams" -ForegroundColor Green

Connect-MicrosoftTeams -ErrorAction Stop
$alreadyConnectedToTeams = $false

} Catch {

Write-Error -Message "Error. Cannot Connect-MicrosoftTeams because $_" -ErrorAction Stop

$connectionAttemptFailed = $true

}

}

if ($alreadyConnectedToTeams -eq $true -and $connectionAttemptFailed -eq $false){

Write-Host ""

Write-Host "Already Connected To MS Teams" -ForegroundColor Green

Write-Host ""

}

if($showExamples){

Write-Host '

#EXAMPLES OF USE

$teams = Get-Team

#GET all teams where Archived status is set to true

$archivedTeams = $($teams | where {$_.Archived -eq $true})

#Set Archived Status of all teams in $archivedTeams to false

$archivedTeams | foreach {Set-TeamArchivedState -GroupId $_.GroupId -Archived:$false}

'

}

}

Export-ModuleMember -Function Connect-XTeams