Skip to content

Instantly share code, notes, and snippets.

@T0biii
Forked from mozziemozz/ClearTeamsClientCache.ps1
Last active March 6, 2024 10:26
Show Gist options
  • Save T0biii/28c5f9e99354cc0797394443156cc0d6 to your computer and use it in GitHub Desktop.
Save T0biii/28c5f9e99354cc0797394443156cc0d6 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Clears the Teams client cache on Windows machines.
.DESCRIPTION
Clears the Teams client cache for whichever Teams version is currently in use while retaining the custom backgrounds.
Authors: Martin Heusser | MVP
Version: 1.0.1
Changelog: 2023-12-01: Initial release
.PARAMETER Name
None.
.INPUTS
None.
.OUTPUTS
None.
.EXAMPLE
. .\ClearTeamsClientCache.ps1
#>
function Clear-TeamsCache {
param (
[string]$Version = "ms-teams"
)
if ($Version -eq "ms-teams") {
$cachePath = "$env:LOCALAPPDATA\Packages\MSTeams_8wekyb3d8bbwe\LocalCache\Microsoft\MSTeams"
}
elseif ($Version -eq "Teams") {
$cachePath = "$env:APPDATA\Microsoft\Teams"
}
$backgroundsPath = "$cachePath\Backgrounds\Uploads"
Write-Host "$Version is in use" -ForegroundColor Cyan
Write-Host "Saving Custom Backgrounds" -ForegroundColor Cyan
$backgrounds = Get-ChildItem -Path $backgroundsPath -ErrorAction SilentlyContinue
$tempFiles = @()
foreach ($background in $backgrounds) {
$tempFiles += @{
FileName = $background.Name
FullName = $background.FullName
FileContent = [System.IO.File]::ReadAllBytes($background.FullName)
}
}
Write-Host "Remove Cache" -ForegroundColor Cyan
Remove-Item -Path $cachePath -Recurse -Force
Write-Host "Add Custom Backgrounds" -ForegroundColor Cyan
New-Item -Path $cachePath -ItemType Directory | Out-Null
New-Item -Path "$cachePath\Backgrounds" -ItemType Directory | Out-Null
New-Item -Path $backgroundsPath -ItemType Directory | Out-Null
foreach ($tempFile in $tempFiles) {
[System.IO.File]::WriteAllBytes($tempFile.FullName, $tempFile.FileContent)
}
}
if (!(Test-Path -Path "$env:APPDATA\Microsoft\Teams") -and !(Test-Path -Path "$env:LOCALAPPDATA\Packages\MSTeams_8wekyb3d8bbwe")) {
Write-Host "Cache folders for either Teams version could not be found." -ForegroundColor Magenta
Read-Host "Press any key to exit..."
Exit
} else {
$teamsProcesses = Get-Process -Name *Teams*
if ($teamsProcesses) {
Write-Host "Stopping Teams processes..." -ForegroundColor Magenta
foreach ($process in $teamsProcesses) {
Stop-Process -Id $process.Id -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds 10
switch ($teamsProcesses[0].ProcessName) {
"ms-teams" {
Clear-TeamsCache -Version $teamsProcesses[0].ProcessName
Write-Host "Start new Teams" -ForegroundColor Cyan
Start-Process ms-teams.exe
}
"Teams" {
Clear-TeamsCache -Version $teamsProcesses[0].ProcessName
Write-Host "Start old Teams" -ForegroundColor Cyan
Set-Location "$env:LOCALAPPDATA\Microsoft\Teams"
Start-Process -File "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe" -ArgumentList '--processStart "Teams.exe"'
}
Default {
}
}
} else {
Write-Host "Neither version of Teams is running. Please start Teams and run this script again." -ForegroundColor Magenta
$checkIfNewTeamsIsInstalled = (Test-Path -Path "$env:LOCALAPPDATA\Packages\MSTeams_8wekyb3d8bbwe")
if ($checkIfNewTeamsIsInstalled -eq $true) {
Write-Host "New Teams is installed" -ForegroundColor Cyan
$oldTeamsLastAccessTime = Get-ItemProperty -Path "$env:LOCALAPPDATA\Microsoft\Teams\Current\Teams.exe" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty LastAccessTimeUtc
$newTeamsLogs = (Get-ChildItem -Path "$env:LOCALAPPDATA\Packages\MSTeams_8wekyb3d8bbwe\LocalCache\Microsoft\MSTeams\Logs" -File -Filter "MSTeams_*.log")[-1]
$newTeamsLogContent = Get-Content -Path $newTeamsLogs.FullName | Out-String
$newTeamsVersionPattern = "LatestVersion:\s*(\d+(\.\d+)*)"
$match = [regex]::Match($newTeamsLogContent, $newTeamsVersionPattern)
$versionNumber = $match.Groups[1].Value
$newTeamsLastAccessTime = Get-ItemProperty -Path "C:\Program Files\WindowsApps\MSTeams_$($versionNumber)_x64__8wekyb3d8bbwe\ms-teams.exe" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty LastAccessTimeUtc
if (!$oldTeamsLastAccessTime -and !$newTeamsLastAccessTime) {
Write-Host "Teams is not installed. Could not get last used time of either Teams version." -ForegroundColor Magenta
$lastUsedVersion = "Teams is not installed."
} elseif ($newTeamsLastAccessTime -gt $oldTeamsLastAccessTime) {
$lastUsedVersion = "New Teams"
} else {
$lastUsedVersion = "Old Teams"
}
} else {
Write-Host "New Teams is not installed"
$lastUsedVersion = "Old Teams"
}
Write-Host "Last used Teams version: $lastUsedVersion" -ForegroundColor Cyan
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment