Skip to content

Instantly share code, notes, and snippets.

@Thadah
Last active February 10, 2023 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thadah/4cc25c8516436b58f98a87408ac37868 to your computer and use it in GitHub Desktop.
Save Thadah/4cc25c8516436b58f98a87408ac37868 to your computer and use it in GitHub Desktop.
Clears Teams cache and restarts the application
# Gontzal Pujana 2023
# Set-ExecutionPolicy Unrestricted -Scope Process -Force -ErrorAction SilentlyContinue; ls -Recurse *.ps*1 | Unblock-File; .\"clearTeamsCache.ps1"
# Stop Teams Process
function Stop-Teams {
$teams = Get-Process -Name Teams -ErrorAction SilentlyContinue
if ($null -ne $teams){
Write-Host "Team Process Found, stopping..." -ForegroundColor Yellow
Stop-Process -Name Teams -Force
Start-Sleep 5
}
$teams = Get-Process -Name Teams -ErrorAction SilentlyContinue
if ($null -ne $teams){
Write-Host "Teams Process Sucessfully Stopped" -ForegroundColor Green
}
else {
Write-Host "Teams Process still hasn't stopped, waiting 2 seconds..." -ForegroundColor Green
Start-Sleep 2
}
}
# Clear Teams Cache
function Clear-Teams-Cache {
param (
[Parameter(Mandatory=$false)]
[string]$TeamsProfile
)
if ($null -eq $TeamsProfile -or $TeamsProfile -eq ""){
$TeamsProfile = $env:APPDATA + "\Microsoft\Teams"
}
else{
$TeamsProfile = $env:LOCALAPPDATA + "\Microsoft\Teams\CustomProfiles\"+$TeamsProfile+"\AppData\Roaming\Microsoft\Teams"
if (-not (Test-Path $TeamsProfile)){
Write-Host "The profile $TeamsProfile does not exist" -ForegroundColor Red
return
}
}
Write-Host "Clearing Teams Disk Cache" -ForegroundColor Yellow
try {
Get-ChildItem -Path $TeamsProfile\"application cache\cache" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"blob_storage" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"databases" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"cache" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"gpucache" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"IndexedDB" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"Local Storage" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"Service Worker\CacheStorage" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"tmp" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
Get-ChildItem -Path $TeamsProfile\"previous" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
New-Item -Path $TeamsProfile\"hooks.json" -ItemType "file" -Force
Write-Host "Teams Disk Cache Cleaned" -ForegroundColor Green
}
catch {
Write-Output $_
}
}
# Restart Teams based on profile
function Restart-Teams {
param (
[Parameter(Mandatory=$false)]
[string]$TeamsProfile
)
Write-Host "Restarting Teams Process..." -ForegroundColor Green
if ($TeamsProfile -ne $null -or $TeamsProfile -ne ""){
$env:USERPROFILE = $env:LOCALAPPDATA + "\Microsoft\Teams\CustomProfiles\"+$TeamsProfile
}
Start-Process -FilePath "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe" -ArgumentList "--processStart Teams.exe" -WindowStyle Hidden
Start-Sleep -Seconds 3
Stop-Process -Id $PID
}
# Main
$challenge = Read-Host "Are you sure you want to delete Teams Cache? This will stop the Teams process (Y/n)"
$challenge = $challenge.ToUpper()
if ($challenge -eq "N") {
Stop-Process -Id $PID
}
$profileChallenge = Read-Host "If using a custom profile, enter the profile name, otherwise press enter"
try {
Stop-Teams
}
catch {
Write-Output $_
}
try {
if ($profileChallenge -ne "") {
Clear-Teams-Cache -TeamsProfile $profileChallenge
Restart-Teams -TeamsProfile $profileChallenge
} else {
Clear-Teams-Cache
Restart-Teams
}
}
catch {
Write-Output $_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment