<# .SYNOPSIS Clears the cache for Microsoft Teams (New Client). .DESCRIPTION This script sets the variable $cacheCleared to $false, indicating that the cache has not been cleared yet. .PARAMETER cacheCleared A boolean variable that indicates whether the cache has been cleared or not. .PARAMETER logFilePath Specifies the path where the log file will be created. .PARAMETER errorFilePath Specifies the path where the error file will be created. .EXAMPLE Clear-NewTeamsCache.ps1 -logFilePath "C:\Logs\CacheLog.txt" -errorFilePath "C:\Logs\ErrorLog.txt" Clears the cache for Microsoft Teams and creates a log file at the specified path. .DISCLAIMER This script is provided as-is without any warranty or support. Use it at your own risk. The author and Microsoft shall not be liable for any damages or losses arising from the use of this script. .NOTES Author: Bhargav Shukla Date: 01/29/2024 #> param ( [bool]$cacheCleared = $false, [string]$logFilePath = "C:\Logs\CacheLog.txt", [string]$errorFilePath = "C:\Logs\ErrorLog.txt" ) # Define cache folder paths $newCacheFolder = "$env:userprofile\appdata\local\Packages\MSTeams_8wekyb3d8bbwe\LocalCache\Microsoft\MSTeams" $classicCacheFolder = "$env:userprofile\appdata\roaming\Microsoft\Teams" # Function to create a file if it doesn't exist function Create-FileIfNotExists { param ( [string]$filePath ) if (-not (Test-Path -Path $filePath)) { New-Item -Path $filePath -ItemType File -Force | Out-Null } } # Function to clear cache function Clear-Cache { param ( [string]$cacheFolder ) Remove-Item -Path $cacheFolder\* -Force -Recurse Write-Host "Cache cleared from $cacheFolder" Add-Content -Path $logFilePath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Cache cleared from $cacheFolder" } # Create log and error files if they don't exist Create-FileIfNotExists -filePath $logFilePath Create-FileIfNotExists -filePath $errorFilePath # Prompt user to clear cache or defer $choice = Read-Host "Do you want to clear the Teams cache now? (Y/N)" if ($choice -eq "Y" -or $choice -eq "y") { try { # Check which version of Teams client is running $teamsProcess = Get-Process -Name "MS-Teams" -ErrorAction SilentlyContinue if ($teamsProcess -eq $null) { $teamsProcess = Get-Process -Name "Teams" -ErrorAction SilentlyContinue } if ($teamsProcess -ne $null) { # Quit Teams application Stop-Process -Id $teamsProcess.Id -Force # Delete cache files based on the client version if ($teamsProcess.Name -eq "MS-Teams") { Clear-Cache -cacheFolder $newCacheFolder } else { Clear-Cache -cacheFolder $classicCacheFolder } # Notify user to start Teams Write-Host "Teams cache cleared. You can now start Teams." # Log the action Add-Content -Path $logFilePath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') Cache Cleared" } else { # Clear cache if Teams is not running $clientMessage = "Teams application is not running." Write-Host $clientMessage Add-Content -Path $logFilePath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $clientMessage" # Verify existence of cache folder(s) and clear cache if (Test-Path -Path $newCacheFolder) { Clear-Cache -cacheFolder $newCacheFolder } else { Clear-Cache -cacheFolder $classicCacheFolder } } } catch { # Log error if cache clearing fails $errorMessage = "Failed to clear cache. Error: $($_.Exception.Message)" Write-Host $errorMessage Add-Content -Path $logFilePath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $errorMessage" } finally { # Perform cleanup operations here if needed } } else { # Log the action Add-Content -Path $logFilePath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Cache Clearing Deferred" }