Step-by-Step Guide on Exporting MS Teams Chat History using PowerShell

In this article, we’ll examine how to access and export the history of Microsoft Teams chat conversations using PowerShell.

Teams chats are stored in a hidden Conversation historyTeam 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.).

You can prevent users of the Microsoft 365 tenant from creating new Teams groups.

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.

Previously, we showed you how to send a message to an MS Teams chat using PowerShell and the Microsoft Graph API.

  1. Create a new appTeamsView app in the Azure Portal (Azure AD -> App registration -> New registration);
  2. Copy the following values: Application (client) ID: your_app_ID Directory (tenant) ID: your_tenant_ID
  3. 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.
  4. Click Grant Admin Consent for…
  5. 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

Learn more about how to connect Microsoft Graph API with PowerShell.

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

You can use certificate-based authentication in Microsoft 365 from your PowerShell script.

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:

<code># 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</code>

You can use the following PowerShell script to get a list of messages and replies from the Teams channel:

<code>$ChannelID="your_chat_id "

$Header =@{Authorization = "Bearer $($Tokenresponse.access_token)"}

$apiUrl = "https://graph.microsoft.com/beta/teams/$TeamsID/channels/$ChannelID/messages"</code>

<code>$Data = Invoke-RestMethod -Uri $apiUrl -Headers $header  -Method Get

$Messages = ($Data | Select-Object Value).Value

class messageData

{</code>

<code>[string]$dateTime

[string]$from

[string]$body

[string]$re

messageData()

{

$this.dateTime = ""

$this.from = ""

$this.body = ""

$this.re = ""

}

}</code>

<code>$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</code>

<code>$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</code>

<code>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</code>

<code>$replyData.re="RE"

$messageSet.Add($replyData)

}

}

$messageSet|ConvertTo-Html | Out-File c:psteams_chat_history.html -Encoding utf8</code>

This script retrieves a list of discussions from the chosen channel, procures a list of responses for each conversation, and produces an HTML record with the complete chat details. The reply field in the discussion table comprises the crucial element RE.

For the scripting code, please visit our GitHub repository at: https://github.com/maxbakhub/winposh/blob/main/teams/export_messages_teams_chat.ps1


Posted

in

, ,

by

Tags: