Find Teams channel name by channel e-mail address

Finding the name of a teams channel when you only have the e-mail address of the channel


You have to manage a Microsoft 365 tenant and then you see somewhere that they used a address like this in a Logic App in Azure and don’t know to which teams channel this e-mail address belongs. You can off course go to every teams channel in teams and find through that way the channel in teams but what when you have more then 100 channels?

and now come scripting in the picture, PowerShell to be precise.

The script

#Requires -Version 3.0
# Make sure to fill in all the required variables before running the script
# Also make sure the AppID used corresponds to an app with sufficient permissions, as follows:
#    Group.Read.All or Directory.Read.All to read all Groups
#    Group.Read.All to read Channel info

param (
    [Parameter(Mandatory = $true, HelpMessage = "'' your tenantID or tenant root domain")]
    [Parameter(Mandatory = $true, HelpMessage = "'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' the GUID of your app.")]
    [Parameter(Mandatory = $true, HelpMessage = "'verylongsecurestring' client secret for the app")]
    [Parameter(Mandatory = $false, HelpMessage = "Do you want a output file")]
    [Parameter(Mandatory = $false, HelpMessage = "Do you want interactive mode")]

#Main script starts here

#Obtain access token
$url = '' + $tenantId + '/oauth2/v2.0/token'

$Scopes = New-Object System.Collections.Generic.List[string]
$Scope = ""

$body = @{
    grant_type    = "client_credentials"
    client_id     = $appID
    client_secret = $client_secret
    scope         = $Scopes

try { 
    Set-Variable -Name authenticationResult -Scope Global -Value (Invoke-WebRequest -Method Post -Uri $url -Debug -Verbose -Body $body)
    $token = ($authenticationResult.Content | ConvertFrom-Json).access_token
catch { $_; return }

if (!$token) {
    Write-Host "Failed to aquire token!"; return
else {
    Write-Verbose "Successfully acquired Access Token"
    #Use the access token to set the authentication header
    Set-Variable -Name authHeader -Scope Global -Value @{'Authorization' = "Bearer $token"; 'Content-Type' = 'application\json' }

#Use the /beta endpoint to fetch a list of all Teams
#Do not switch to as we need the Group proxy address details
$uri = "`$filter=resourceProvisioningOptions/Any(x:x eq `'Team`')&`$select=id,displayName,mail,proxyAddresses,resourceBehaviorOptions,resourceProvisioningOptions,visibility"
$result = Invoke-WebRequest -Headers $AuthHeader -Uri $uri -Verbose:$VerbosePreference
$teams = ($result.Content | ConvertFrom-Json).Value

#iterate over each Team and gather channel information
$output = @()
foreach ($team in $teams) {
    $uri = "$($"
    $result = Invoke-WebRequest -Headers $AuthHeader -Uri $uri -Verbose:$VerbosePreference
    $channels = ($result.Content | ConvertFrom-Json).Value

    #$channels | select displayName,email,id,webUrl
    foreach ($channel in $channels) {
        $chaninfo = New-Object psobject
        $chaninfo | Add-Member -MemberType NoteProperty -Name "Id" -Value $
        $chaninfo | Add-Member -MemberType NoteProperty -Name "Team" -Value $team.displayName
        $chaninfo | Add-Member -MemberType NoteProperty -Name "TeamId" -Value $
        $chaninfo | Add-Member -MemberType NoteProperty -Name "TeamEmailAddresses" -Value $($team.proxyAddresses -join ",")
        $chaninfo | Add-Member -MemberType NoteProperty -Name "Visibility" -Value $team.visibility
        $chaninfo | Add-Member -MemberType NoteProperty -Name "Channel" -Value $channel.displayName
        $chaninfo | Add-Member -MemberType NoteProperty -Name "ChannelEmail" -Value (& { If ($ { $ } Else { "N/A" } })
        $output += $chaninfo


#return the output
$global:varTeamChannels = $output | Select-Object Team, Visibility, Channel, ChannelEmail, TeamEmailAddresses, TeamId

if ($csv) {
    $output | Select-Object * | Export-Csv -Path "$((Get-Date).ToString('yyyy-MM-dd_HH-mm-ss'))_TeamsChannels.csv" -NoTypeInformation -Encoding UTF8 -UseCulture

if ($interactive) {
return $global:varTeamChannels | Where-Object {$_.ChannelEmail -ne $Null} | Out-GridView -OutputMode Single -Title 'Find the e-mail address of a teams channel'

You can also download the script from my GitHub repro

comments powered by Disqus