Export Microsoft Teams Information using Graph API


As we already know that Microsoft Teams PowerShell does not provide cmdlets to get detailed information from Microsoft Teams.  It may evolve over next few months but we do have Microsoft Graph API that provides new has endpoints that you can use to export information about Team, Channels, Messages, Members and owners etc.  The sample below does some of the basic functions.  It all the teams and then try to get some information to an array of objects.  This is no way a complete sample but a good starting point.   It might help you get started with Teams.

I am using PNPOnline Powershell Module to get connect to Micorosft Graph and Get the Access token.  There couple of different ways to do that but PNP makes it lot easier.  Make sure you install the SharePointPnPPowerShellOnline module.

You can create an App in Azure Active Directory that has the Group.Read.all and User.Read.All permissions to make it easy for you.  Make sure you click on Grant permission after adding the Graph API permission.

$scopes = @(“Group.Read.All”,”User.ReadWrite.All”, “Directory.Read.All”,”Reports.Read.All”)
$scopes = $null

$ApplicationID = “”
$Password = ”
$appaaddomain = ‘tenant.onmicrosoft.com’
 
$GraphURL = “https://graph.microsoft.com/beta”
$url = “$GraphURL/groups?`$filter=resourceProvisioningOptions/Any(x:x eq ‘Team’)”

#Establish connection
If($scopes.Length -gt 0){
    Connect-PnPOnline -Scopes $scopes
} elseif($ApplicationID.Length -gt 0) {
    Write-Host “Connecting using Application” -ForegroundColor Yellow
    Connect-PnPOnline -AppId $ApplicationID -AppSecret $Password -AADDomain $appaaddomain
} else {
    write-host ‘Connection issue’ -ForegroundColor Red
    exit
}
 
#Get token
$token = Get-PnPAccessToken
 
#Call graph
if($token){
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = “Bearer $token”}
} else {
    write-host ‘Token issue’ -ForegroundColor Red
    exit
}

$TeamsData = $null
$TeamsData = @()

#Parse data
if($response){
    foreach($r in $response.value){
        if($r.resourceProvisioningOptions -eq ‘Team’){
           
            $teamDataObj = New-Object -TypeName PSObject
           
            $GroupClassification = $r.classification
            $GroupCreatedDateTime = $r.createdDateTime
            $visibility = $r.visibility
            $TeamName = $r.displayname
            write-host “Team Name:$TeamName Classification:$GroupClassification CreatedOn:$GroupCreatedDateTime” -ForegroundColor Yellow
            $TeamID = $r.id
            $TeamUrl =  “$GraphURL/groups/$TeamID/members”
            $TeamsOwnerUrl =  “$GraphURL/groups/$TeamID/owners”
            $ChannelsUrl =  “$GraphURL/groups/$TeamID/channels”
           
            $teamDataObj | Add-Member -Type NoteProperty -Name TeamName -Value $TeamName
            $teamDataObj | Add-Member -Type NoteProperty -Name Createdon -Value $GroupCreatedDateTime
            $teamDataObj | Add-Member -Type NoteProperty -Name Classification -Value $GroupClassification
            $teamDataObj | Add-Member -Type NoteProperty -Name Visibility -Value $visibility
           
            if($token){
                $rTeam = Invoke-RestMethod -Uri $TeamUrl -Headers @{Authorization = “Bearer $token”}
                $rTeamsOwners = Invoke-RestMethod -Uri $TeamsOwnerUrl -Headers @{Authorization = “Bearer $token”}
                $rTeamChanel = Invoke-RestMethod -Uri $ChannelsUrl -Headers @{Authorization = “Bearer $token”}
            } else {
                write-host ‘Token issue’ -ForegroundColor Red
                exit
            }
            $TeamOwnersAll = “”
            if($rTeamsOwners)
            {
                Write-Host ”    Team Owners:”
                foreach($owner in $rTeamsOwners.value)
                {
                    $OwnerName = $owner.displayName
                    $OwnerEmail = $owner.mail
                    Write-Host ”        $OwnerName”
                    $TeamOwnersAll +=”$OwnerEmail#”
                }
         
            }
            $teamDataObj | Add-Member -Type NoteProperty -Name Owners -Value $OwnerEmail
            if($rTeam)
            {
                $TeamMembersAll = “”
                Write-Host ”    Team Members:”
                foreach($member in $rTeam.value)
                {
                    $TeamMember = $member.displayName
                    $TeamMemberEmail = $member.mail
                    $TeamMemberRole = $member.assignedLicenses
                    Write-Host ”        $TeamMember”
                    $TeamMembersAll +=”$TeamMemberEmail#”
                }
         
            }
            $teamDataObj | Add-Member -Type NoteProperty -Name TeamMembers -Value $TeamMembersAll
            if($rTeamChanel)
            {
                Write-Host ”    Channels”
                $AllChannels = “”
                foreach($channel in $rTeamChanel.value)
                {
                    $ChannelName = $channel.displayName
                    $ChannelId = $channel.id
                    Write-Host ”        Channels: $ChannelName”
                    $AllChannels +=”$ChannelName#”
                   
           
                    #/teams/{id}/channels/{id}/messages
           
                    $ChannelsMessageUrls =  “$GraphURL/teams/$TeamID/channels/$ChannelId/messages”
           

                    if($token){
                        $rMessages = Invoke-RestMethod -Uri $ChannelsMessageUrls -Headers @{Authorization = “Bearer $token”}
                    } else {
                        write-host ‘Token issue’ -ForegroundColor Red
                        exit
                    }
                   
                    $MessageCount = 0
                    $Replies = 0
           
                    if($rMessages)
                    {
                        foreach($Message in $rMessages.value)
                        {
                            $MessageId = $Message.id
                            $messageBody = $Message.body
                            $MessagecreatedDateTime = $message.createdDateTime
                            $MessageCount++;
                            Write-Host ”        Message: $messageBody $MessageId $MessagecreatedDateTime”
                            $ChannelsRepliesUrls =  “$GraphURL/teams/$TeamID/channels/$ChannelId/messages/$MessageId/replies”
                            if($token){
                                $rReplies = Invoke-RestMethod -Uri $ChannelsRepliesUrls -Headers @{Authorization = “Bearer $token”}
                                if($rReplies)
                                {
                                    foreach($rReplie in $rReplies.Value)
                                    {
                                        $rreplyId = $rReplie.body
                                        $rcreatedDateTime = $rReplie.createdDateTime
                                        $Replies++;
                                        Write-Host ”            Reply:$rcreatedDateTime”
                                    }
                                }
                            } else {
                                write-host ‘Token issue’ -ForegroundColor Red
                                exit
                            }
                        }
                    }
       
                }
                $teamDataObj | Add-Member -Type NoteProperty -Name Channels -Value $AllChannels
                $teamDataObj | Add-Member -Type NoteProperty -Name MessageCount -Value $MessageCount
                $teamDataObj | Add-Member -Type NoteProperty -Name RepliesCount -Value $Replies
            }
           
            $TeamsData += $teamDataObj
       
            #Do fancy stuff in here
        } else {
            write-host $r.displayname “is a regular O365 Group” -ForegroundColor Green
        }
    }
} else {
    write-host ‘Response issue’ -ForegroundColor Red
}

$TeamsData | Out-GridView
$TeamsData | Export-Csv -Path C:\temp\TeamsInfo.csv –NoTypeInformation

Technet Link for Code.

https://gallery.technet.microsoft.com/Export-Teams-Information-2ea6b3db