Clear-TeamsCache.ps1
<#PSScriptInfo
.VERSION 1.0 .GUID b72d1999-8897-4812-b53c-bedf4a6b80bb .AUTHOR Florian Salzmann .COMPANYNAME scloud.work .COPYRIGHT .TAGS Windows Teams .LICENSEURI https://github.com/FlorianSLZ/IntuneDeviceInventory/blob/main/LICENSE.md .PROJECTURI https://github.com/FlorianSLZ/scloud .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES Version 1.0: Original published version. #> <# .SYNOPSIS Delete Teams Chache for the current user or all users on the computer. .DESCRIPTION Delete Teams Chache for the current user or all users on the computer. .PARAMETER All Switch to specify that the Teams cache for all users should be cleared. .EXAMPLE .\Clear-TeamsCache.ps1 .EXAMPLE .\Clear-TeamsCache.ps1 -All #> param( [parameter(Mandatory = $false, HelpMessage = "Switch to specify that the Teams cache for all users should be cleared.")] [ValidateNotNullOrEmpty()] [switch]$All ) if($All){ # Self-elevate the script (admin rights) Write-Verbose "Checking if process is running as admin ..." if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { Write-Error "Please run the -All parameter only with admin rights" break } Write-Verbose "Clearing Teams cache for all Users" Write-Verbose "Check if any Teams process is running ..." if(Get-Process Teams -ErrorAction SilentlyContinue){ Write-Verbose "Stop all Teams processes ..." Get-Process Teams | Stop-Process -Force Start-Sleep -s 3 # short timout, cause files are bloked } Write-Verbose "Clearing all Teams caches ..." Get-ChildItem "C:\Users\*\AppData\Roaming\Microsoft\Teams\*" -directory | Where-Object name -in ('application cache','blob storage','databases','GPUcache','IndexedDB','Local Storage','tmp') | ForEach-Object{Remove-Item $_.FullName -Recurse -Force } }else{ Write-Verbose "Clearing Teams cache for current Users" Write-Verbose "Check if Teams is running ..." if(Get-Process Teams -ErrorAction SilentlyContinue){ Write-Verbose "Stoping Teams process ..." Stop-Process -Name Teams -Force Start-Sleep -s 3 # short timout, cause files are bloked } Write-Verbose "Clearing Teams cache ..." Get-ChildItem -Path "$env:USERPROFILE\AppData\Roaming\Microsoft\Teams" -Directory | Where-Object{$_ -in ('Cache','databases','blob_storage','IndexedDB','GPUCache','Local Storage','tmp','')} | ForEach-Object{Remove-Item $_.FullName -Recurse -Force} } |