In this article, we’ll look at how to access and export the history of Microsoft Teams chat conversations using PowerShell.
Teams chats are stored in a hidden Conversation history\Team Chat folder in a shared mailbox, which is automatically created when you create a new Microsoft 365 group (this will instantly create a Teams group, a website, a SharePoint Online library, a Yammer group, etc.).
However, you cannot access the protected folder with a Teams chat history using Outlook or another app. You can export the contents of an Exchange Online mailbox to a PST file using Content Search in the Security and Compliance Center and then connect the PST file in Outlook. But it is not too convenient. It’s much easier to use PowerShell to get a list of Teams chat messages.
To connect to a Microsoft 365 tenant, we will use the Microsoft Graph API.
- Create a new appTeamsView app in the Azure Portal (Azure AD -> App registration -> New registration);
- Copy the following values:
Application (client) ID:your_app_ID
Directory (tenant) ID:your_tenant_ID
- Go to API Permissions, click Microsoft Graph -> Application permissions -> Channel -> select Channel.Basic.ReadAll and ChannelMessage.Read.All. Add the permission Group -> Group.Read.All. Grant the same permissions in Microsoft Graph -> Delegated permissions and also in Directory.AccessAsUser.All.
- Click Grant Admin Consent for…
- Then create a secret to access the app. Go to Certificates & secrets -> New client secrets, specify the key name and its validity period.
Copy the value from the Value field:
Value:your_secret
Then you can connect to Microsoft Entra ID (Azure AD) from PowerShell and get an access token.
$clientId = "your_app_ID" $tenantName = "yourtenant.onmicrosoft.com" $clientSecret = "your_secret" $resource = "https://graph.microsoft.com/" $Username = "[email protected]" $Password = "yourpassword" $ReqTokenBody = @{ Grant_Type = "Password" client_Id = $clientID Client_Secret = $clientSecret Username = $Username Password = $Password Scope = "https://graph.microsoft.com/.default" } $TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
Now you can get various data from your Microsoft 365 tenant.
List the Teams in your tenant:
#Getting all Teams $header= @{Authorization = "Bearer $($TokenResponse.access_token)"} $BaseURI = "https://graph.microsoft.com/beta" $AllMicrosoftTeams = (Invoke-RestMethod -Uri "$($BaseURI)/groups?'$filter=resourceProvisioningOptions/Any(x:x eq 'Team')" -Headers $header -Method Get -ContentType "application/json").value $AllMicrosoftTeams| FT id, DisplayName,Description
Then display a list of channels in the Teams group by its ID:
# List channels in Team $TeamsID="your_team_id" $TeamsChannels = (Invoke-RestMethod -Uri "$($BaseURI)/teams/$($TeamsID)/channels" -Headers $Header -Method Get -ContentType "application/json").value $TeamsChannels | FT id, DisplayName,Description
You can use the following PowerShell script to get a list of messages and replies from the Teams channel:
$ChannelID="your_chat_id " $Header =@{Authorization = "Bearer $($Tokenresponse.access_token)"} $apiUrl = "https://graph.microsoft.com/beta/teams/$TeamsID/channels/$ChannelID/messages" $Data = Invoke-RestMethod -Uri $apiUrl -Headers $header -Method Get $Messages = ($Data | Select-Object Value).Value class messageData { [string]$dateTime [string]$from [string]$body [string]$re messageData() { $this.dateTime = "" $this.from = "" $this.body = "" $this.re = "" } } $messageSet = New-Object System.Collections.ArrayList; foreach ($message in $Messages) { $result = New-object messageData $result.DateTime=Get-Date -Date (($message).createdDateTime) -Format 'yyyy/MM/dd HH:mm' $result.from = $message.from.user.displayName $result.body = $message.body.content $messageSet.Add($result) #parsing replies $repliesURI = "https://graph.microsoft.com/beta/teams/" + $TeamsID + "/channels/" + $ChannelID + "/messages/" + $message.ID + "/replies?`$top100" $repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI -Headers $header foreach ($reply in $repliesResponse.value) { $replyData = New-Object messageData $replyData.dateTime = Get-Date -Date (($reply).createdDateTime) -Format 'yyyy/MM/dd HH:mm' $replyData.from = $reply.from.user.displayName $replyData.body= $reply.body.content $replyData.re="RE" $messageSet.Add($replyData) } } $messageSet|ConvertTo-Html | Out-File c:\ps\teams_chat_history.html -Encoding utf8
This script gets a list of conversations from the specified channel, gets a list of replies for each conversation, and generates an HTML file with the full contents of the chat. The replies to the discussion in the table contain the key field RE
.
Check out our GitHub repository for this script code: https://github.com/maxbakhub/winposh/blob/main/teams/export_messages_teams_chat.ps1
1 comment
Is Teams chat history also saved in on-premise Exchange server if I’m using hybrid model?