Teams Conversations : Powershell Combing and Formatting

If you have to export teams messages in a corporation setting, and by corporation I mean, you have a proper enterprise style license and it’s not your personal teams chats, that you conveniently accomplish with the export my data tool with Microsoft.

Anyway, going back to the task hand if you don’t have the premium export capabilities, you will get the messages like they are stored in Outlook, which is an individual message at a time, Which is not very good if you need to review the conversational history for whatever reason, so once you’ve exported the messages you will need to use PowerShell to make the messages readable.

Export Files from PST

xxx

Exported and unreadable

So, lets consider you have a folder with chats for one person and the content looks something like, in the first file you have the first message:

From:        Lee Croucher                                 
SentOn:      09/11/2023 16:15:25                           
To:          Bear Claws
Importance:  Medium                                       
   I now have the location of the secret honey cave....

Then in the second file you have the second message:

From:        Lee Croucher                                 
SentOn:      05/10/2023 09:04:32                           
To:          Bear Claws
Importance:  Medium                                        
   NO, no ignore that the location of where castle grey skull is located

You could have hundreds or thousands of these files and you would need to open every single file to get the conversation in full, that is not acceptable.

Combine all data into one file

Right, so first we need to combine all the chat into one file which you can do with this script, this will take all the files in the folder and combine then into one large text file, so update the <path_to_data> with the location of this folder.

Note : If you wish to move the folder where the outputted file is crated updated the $outputfilepath

# Define the directory where the Teams conversation files are located
$directoryPath = "<path_to_data>"

# Get all conversation files from the directory
$conversationFiles = Get-ChildItem -Path $directoryPath -Filter "*.txt"

# Define the path for the output combined file
$outputFilePath = "<path_to_data>\CombinedFile.txt"

# Create an empty array to store the content of each file
$combinedContent = @()

# Loop through each conversation file, read its content, and add it to the combinedContent array
foreach ($file in $conversationFiles) {
    $content = Get-Content $file.FullName
    $combinedContent += $content
}

# Combine the content of all files into one big file
$combinedContent | Out-File -FilePath $outputFilePath

Write-Host "Combined file created at $outputFilePath"

Format the combined data

Now you need to format the text file you have so it reads nicer and each messages has a line return between it which makes it absolutely more readable, this is done with this script, remember to update the paths in bold:

Note : This script will output a file called Formatted.txt to the folder you specify

# Define the path to the combined file
$combinedFilePath = "<path_to_data>\CombinedFile.txt"

# Read the content of the combined file
$combinedContent = Get-Content -Path $combinedFilePath

# Define a regular expression pattern to match the "from:" line
$fromPattern = "from:"

# Create an array to store the modified message content
$modifiedMessages = @()

# Loop through each line in the combined content
foreach ($line in $combinedContent) {
    # Check if the line contains "from:" (ignoring case)
    if ($line -match $fromPattern) {
        $modifiedMessages += $line  # Add the "from:" line as is
        $modifiedMessages += "`r`n" # Add a new line after the "from:" line
    } else {
        $modifiedMessages += $line  # Add other lines as is
    }
}

# Write the modified message content back to the combined file
$modifiedMessages | Out-File -FilePath "<path_to_data>\Formatted.txt"

Write-Host "Modified message content saved to $combinedFilePath"

Format the combined data + Keyword Search

If you wish to do the same as the above script but wish to look for keywords in the chat history then you can use this script remember to update the <path_to_data> and the keywords to your requirements.

# Define the path to the combined file
$combinedFilePath = "<path_to_data>\CombinedFile.txt"

# Define a list of keywords to search for
$keywords = @("keyword1", "keyword2", "keyword3")  

# Read the content of the combined file
$combinedContent = Get-Content -Path $combinedFilePath

# Create an array to store the extracted messages containing the specified keywords
$extractedMessages = @()

# Loop through each line in the combined content
for ($i = 0; $i -lt $combinedContent.Count; $i++) {
    $line = $combinedContent[$i]
    
    # Check if the line contains any of the specified keywords (case-insensitive and whole word match)
    foreach ($keyword in $keywords) {
        if ($line -match "\b$keyword\b" -and $line -notmatch "^[\s]*$") {
            # Add the current line to the extracted messages array
            $extractedMessages += $line
            
            # Check and add the subsequent lines if they're not empty or just whitespace
            for ($j = $i + 1; $j -lt $combinedContent.Count; $j++) {
                $nextLine = $combinedContent[$j]
                if ($nextLine -notmatch "^[\s]*$") {
                    $extractedMessages += $nextLine
                } else {
                    break
                }
            }
            
            # Break the loop once a keyword match is found in the line
            break
        }
    }
}

# Write the extracted messages containing the specified keywords back to the file
$extractedMessages | Out-File -FilePath "<path_to_data>\FormattedKeywords.txt"

Write-Host "Extracted messages containing specified keywords saved to out.txt"

Previous Post Next Post

Ω†Ω…ΩˆΨ°Ψ¬ Ψ§Ω„Ψ§ΨͺΨ΅Ψ§Ω„