Public/Clear-TeamsCache.ps1
<#
.SYNOPSIS Clears Microsoft Teams cache files for all users. .NOTES Author: Tom de Leeuw Website: https://ucsystems.nl / https://tech-tom.com #> function Clear-TeamsCache { [CmdletBinding(ConfirmImpact='Medium', SupportsShouldProcess = $true)] param( # Enabling this parameter will skip confirmation. [Parameter(ValuefromPipeline = $True)] [Switch] $Force ) begin { # Verify if running as Administrator Assert-RunAsAdministrator # Start timer $StopWatch = [System.Diagnostics.Stopwatch]::StartNew() # Get disk space for comparison afterwards $Before = Get-DiskSpace # Get all user folders, exclude administrators and default users $Users = Get-UserFolders # Folders to clean up $Folders = @( 'AppData\Roaming\Microsoft\Teams\blob_storage', 'AppData\Roaming\Microsoft\Teams\databases', 'AppData\Roaming\Microsoft\Teams\cache', 'AppData\Roaming\Microsoft\Teams\gpucache', 'AppData\Roaming\Microsoft\Teams\Indexeddb', 'AppData\Roaming\Microsoft\Teams\Local Storage', 'AppData\Roaming\Microsoft\Teams\tmp' ) } process { Write-Verbose "Starting Teams Cache cleanup process..." # Prompt for user verification before continuing if ( -not ($Force)) { Get-UserConfirmation -WarningMessage "This will stop all running Teams processes!" } # Kill Teams process(es) try { Write-Verbose "Killing Teams process(es)..." Get-Process -ProcessName 'Teams' -ErrorAction 'Stop' | Stop-Process -Force -Confirm:$false } catch [Microsoft.PowerShell.Commands.ProcessCommandException] { Write-Information "No running $($_.Exception.ProcessName) processes." } catch { Write-Error "$($_.Exception.Message)" } # Start cleaning files ForEach ($UserName In $Users) { $FilesToClean = ForEach ($FolderName In $Folders) { $FolderToClean = "$env:SYSTEMDRIVE\Users\$Username\$FolderName" If (Test-Path -Path $FolderToClean) { try { Get-ChildItem -Path $FolderToClean -Recurse -Force -ErrorAction 'SilentlyContinue' } catch { Write-Error "$($Username): $($_.Exception.Message)" } } } $FilesToClean | ForEach-Object { if ($_.PSIsContainer) { Write-Information "Cleaning directory: $($_.FullName)" } Remove-Item $_.FullName -Recurse -Force -ErrorAction 'SilentlyContinue' } Write-Information "Finished removing Teams Cache files for $UserName." } } end { New-CleanupReport -Name "TeamsCache" } } |