Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<#
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant
You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code
form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software
product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the
Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims
or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
Please note: None of the conditions outlined in the disclaimer above will supercede the terms and conditions contained
within the Premier Customer Services Description.
#>
$Cleanup = Read-Host -Prompt 'Do you wish to initiate the MSTeams cache cleanup process? (y|n)'
If($Cleanup -eq 'y')
{ #Begin IF1
$message = "This process will close out of Microsoft Teams - any ongoing Teams calls/meetings will be disconnected. Are you sure?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$choice=$host.ui.PromptForChoice($title, $message, $options, 1)
if ($choice -eq 0)
{
# get Teams process
Write-host "Closing Microsoft Teams, Standby..." -ForegroundColor Green
$msTeams = Get-Process teams -ErrorAction SilentlyContinue
#Detect if Teams is running and if so, kill task
$MicrosoftTeamsopen = $msTeams
If ($MicrosoftTeamsopen -ne $null)
{
$msTeams | Stop-Process -Force
}
#Sleep for 10 seconds to free up in use files
Write-Host "Waiting for process to close completely...standby"
Start-Sleep -s 10
#define base path to Teams app data folder
Write-host "Collecting environment variables and building folder locations, Standby..." -ForegroundColor Green
$appdataFolder = Get-ChildItem Env:APPDATA | Select Value | ft -HideTableHeaders| Out-String
$appdataFolder = $appdataFolder -replace "`t|`n|`r",""
$msteamsAppDataFolder = $appdataFolder + '\Microsoft\Teams'
#folder paths to be cleaned up
$teamsAppCacheFolder = $msteamsAppDataFolder + '\Application cache\cache\'
$teamsBlobStorageFolder = $msteamsAppDataFolder + '\blob_storage\'
$teamsCacheFolder = $msteamsAppDataFolder + '\Cache\'
$teamsdatabaseFolder = $msteamsAppDataFolder + '\databases\'
$teamsgpuCacheFolder = $msteamsAppDataFolder + '\GPUcache\'
$teamsindexDbFolder = $msteamsAppDataFolder + '\IndexedDB\'
$teamslocalStorageFolder = $msteamsAppDataFolder + '\Local Storage\'
$teamstmpFolder = $msteamsAppDataFolder + '\tmp'
#Create new, empty array
$msTeamsPathArray = @()
#Enumerate script-defined varibles beginning with "teams" from previous step
$msTeamsLocationList = Get-Variable | Where-Object {$_.Name -like 'teams*'} | select value
#Add teams folder locations into previously defined empty array
$msTeamsPathArray += $msTeamsLocationList
#loop through each folder path in the array and recursively delete items
Write-host "Initiating Teams folder/cache cleanup, Standby..." -ForegroundColor Green
Foreach ($object in $msTeamsPathArray)
{
$folderExist = Test-Path $object.value
Write-host "Location $object Detected? $folderExist"
If ($folderExist -eq $true)
{
Write-host "Folder $object detected. Deleting ..." -ForegroundColor Yellow
Remove-Item -Path $object.value -Recurse -Force
}
}
#loop through each folder path in the array and recursively delete items
Write-host "Performing folder removal check, Standby..." -ForegroundColor Green
Foreach ($object in $msTeamsPathArray)
{
$folderExist = Test-Path $object.value
Write-host "Location $object Detected? $folderExist"
If ($folderExist -eq $true)
{
Write-host "Additional cleanup required. Folder $object detected. Deleting ..." -ForegroundColor Yellow
Remove-Item -Path $object.value -Recurse -Force
}
}
#Remove msteams entries in credman for user
Write-host "Removing ADAL cache credentials from Windows Credman, Standby..." -ForegroundColor Green
$keys = cmdkey /list
$targets = $keys | Select-string -Pattern "msteams"
$targets | Sort-Object
Foreach($target in $targets)
{
$target = $target -replace "Target: ",""
$target = $target -replace " ",""
write-host $target
cmdkey -delete:$target
}
#Finish
Write-host "Finished - Please restart Microsoft Teams from Start menu." -ForegroundColor Cyan
}
}
#FOR TESTING ONLY. Remove all declared variables. Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment