From 6ab4155eebeb474a6fb224c9b7673fe9f1f8b12c Mon Sep 17 00:00:00 2001 From: Shiro Date: Sun, 17 Sep 2023 19:14:16 +0200 Subject: [PATCH] Refactor: Fixed the typing issues --- README.md | 1 + clearTeams.ps1 | 71 ++++++++++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 4466bc4..22ed42e 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ This is a PowerShell script that is designed to clean the cache of Microsoft Tea ⚠️ **Script requires administrator privileges to run.** ## Usage + - Launch `clearTeams.ps1` - Grant administrator privileges - The script will close any active Teams process and clear the Teams cache diff --git a/clearTeams.ps1 b/clearTeams.ps1 index c43576d..03852d7 100644 --- a/clearTeams.ps1 +++ b/clearTeams.ps1 @@ -1,47 +1,50 @@ param([switch]$Elevated) + function Check-Admin { -$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) -$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) -} -if ((Check-Admin) -eq $false) { -if ($elevated) -{ -exit -} - -else { -Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) + $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) + $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) } -exit + +if ((Check-Admin) -eq $false) { + if ($elevated) { + exit + } else { + Start-Process (Get-Process -Id $PID).ProcessName -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) + } + + exit } Write-Host "Closing the Teams process" -ForegroundColor Yellow + try{ -Get-Process -ProcessName Teams | Stop-Process -Force -Write-Host "Closed the Teams process" -ForegroundColor Green -}catch{ -Write-Host "No Teams process detected" -ForegroundColor Green -echo $_ + Get-Process -ProcessName Teams -ErrorAction Ignore | Stop-Process -force +} catch { + Write-Host "No Teams process detected" -ForegroundColor Green } -Write-Host "Clearing a Teams cache" -ForegroundColor Yellow + +Write-Host "Cleaning the Teams cache" -ForegroundColor Yellow + try{ -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\Cache" | Remove-Item -Confirm:$false -force -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\blob_storage" | Remove-Item -Confirm:$false -force -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\databases" | Remove-Item -Confirm:$false -force -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\gpucache" | Remove-Item -Confirm:$false -force -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\Indexeddb" | Remove-Item -Confirm:$false -recurse -force -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\Local Storage" | Remove-Item -Confirm:$false -recurse -force -Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\tmp" | Remove-Item -Confirm:$false -force -Write-Host "Roaming cache cleared" -ForegroundColor Green -}catch{ -echo $_ + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\Cache" | Remove-Item -Confirm:$false -force + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\blob_storage" | Remove-Item -Confirm:$false -force + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\databases" | Remove-Item -Confirm:$false -force + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\gpucache" | Remove-Item -Confirm:$false -force + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\Indexeddb" | Remove-Item -Confirm:$false -recurse -force + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\Local Storage" | Remove-Item -Confirm:$false -recurse -force + Get-ChildItem -Path $env:APPDATA\"Microsoft\Teams\tmp" | Remove-Item -Confirm:$false -force + Write-Host "Roaming cache cleaned" -ForegroundColor Green +} catch { + echo $_ } + $challenge = Read-Host "Launch Teams? (Y/N)?" $challenge = $challenge.ToUpper() -if ($challenge -eq "N"){ -Stop-Process -Id $PID -}elseif ($challenge -eq "Y"){ -Write-Host "Launching Teams" -ForegroundColor Yellow -Start-Process -FilePath $env:LOCALAPPDATA\Microsoft\Teams\current\Teams.exe -Stop-Process -Id $PID + +if ($challenge -eq "N") { + Stop-Process -Id $PID +} elseif ($challenge -eq "Y") { + Write-Host "Launching Teams" -ForegroundColor Yellow + Start-Process -FilePath $env:LOCALAPPDATA\Microsoft\Teams\current\Teams.exe + Stop-Process -Id $PID }