⚡ PowerShell 5.1+

Backup The Microsoft Teams cache to Zip

Zips up the current contents of the Microsoft Teams cache before you run a cleanup against it.

By WindowsScripting.com · 1.3 KB
Backup-TeamsCacheToZip.ps1
<#
.SYNOPSIS
    Backs up the Microsoft Teams cache to a timestamped zip archive before you clear it.

.DESCRIPTION
    Compresses the current contents of the Microsoft Teams cache into a zip file in
    -DestinationPath, so you have a copy before running a cleanup script
    against it.

.PARAMETER DestinationPath
    Folder to save the zip archive into. Created if it doesn't exist.

.EXAMPLE
    .\Backup-TeamsCacheToZip.ps1 -DestinationPath D:\Backups
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [string]$DestinationPath
)

$sourcePath = "$env:APPDATA\Microsoft\Teams\Cache"

if (-not (Test-Path $sourcePath)) {
    Write-Host "$sourcePath does not exist on this machine." -ForegroundColor Yellow
    return
}

if (-not (Test-Path $DestinationPath)) {
    New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
}

$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$zipPath = Join-Path $DestinationPath "teamscache_$timestamp.zip"

Compress-Archive -Path (Join-Path $sourcePath '*') -DestinationPath $zipPath -CompressionLevel Optimal -ErrorAction SilentlyContinue

if (Test-Path $zipPath) {
    Write-Host "Backed up to $zipPath" -ForegroundColor Green
} else {
    Write-Host 'Nothing was backed up (folder may have been empty).' -ForegroundColor Yellow
}