Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Azure and Microsoft 365 / How to Export MS Teams Chat History with PowerShell

March 12, 2024 Azure and Microsoft 365PowerShell

How to Export MS Teams Chat History with PowerShell

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.).

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… Grant app permissions in Azure
  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

Create secret for Azure app

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:

# 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

PowerShell: List channels in a team

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

Export Microsoft Teams chat to html with PowerShell

1 comment
4
Facebook Twitter Google + Pinterest
previous post
Enable All CPU Cores on Windows 10 and 11
next post
Managing VMware Infrastructure with Ansible

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

How to Write Logs to the Windows Event...

March 3, 2025

1 comment

Lab December 20, 2023 - 9:20 pm

Is Teams chat history also saved in on-premise Exchange server if I’m using hybrid model?

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • Checking User Sign-in Logs in Entra ID (Microsoft 365)
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Blank Sign-in Screen in Office 365 Apps (Outlook, Teams, etc.)
  • Removing Built-in Teams Chat in Windows 11
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top