Created
August 23, 2021 08:10
-
-
Save hmunyeku/0d0b565728aa1fdafb12311a5d1f8746 to your computer and use it in GitHub Desktop.
Clearing , Uninstall and Reinstall Teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Clearing , Uninstall and Reinstall Teams by Herve MUNYEKU (hmunyeku@gmail.com) | |
#Function to generate a timestamp that is added to the log file | |
function Get-TimeStamp { | |
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date) | |
} | |
#Function to generate a log file | |
if ((Test-Path -Path "c:\Temp\Teams" -PathType Container) -ne $true ) {mkdir "c:\Temp\Teams" | Out-Null} | |
$LogFile = "c:\Temp\Teams\Teams.log" | |
Function LogWrite | |
{ | |
Param ([string]$logstring) | |
Add-content $Logfile -value "$(Get-Timestamp) $logstring" | |
} | |
LogWrite "** STARTING Uninstall MS Teams Script **" | |
$clearCache = Read-Host "Do you want to delete the Teams Cache (Y/N)?" | |
$clearCache = $clearCache.ToUpper() | |
LogWrite "Do you want to delete the Teams Cache (Y/N)? --> $clearCache" | |
$unistall = Read-Host "Do you want to uninstall Teams completely (Y/N)?" | |
$unistall = $unistall.ToUpper() | |
LogWrite "Do you want to uninstall Teams completely (Y/N)? --> $unistall" | |
$reinstall = Read-Host "Do you want to reinstall after desinstallation (Y/N)?" | |
$reinstall = $reinstall.ToUpper() | |
if ($reinstall -eq "Y"){ | |
$uptodate = Read-Host "Do you want to download a up-to-date version for installation (Y/N)?" | |
$uptodate = $uptodate.ToUpper() | |
LogWrite "Do you want to download a up-to-date version for installation (Y/N)? --> $uptodate" | |
} | |
$deleteInstall = Read-Host "Do you want to delete install file after installation (Y/N)?" | |
$deleteInstall = $deleteInstall.ToUpper() | |
LogWrite "Do you want to delete install file after installation (Y/N)? --> $deleteInstall" | |
if ($clearCache -eq "Y"){ | |
Write-Host "Stopping Teams Process" -ForegroundColor Yellow | |
LogWrite "Stopping Teams Process" | |
try{ | |
Get-Process -ProcessName Teams | Stop-Process -Force | |
Start-Sleep -Seconds 3 | |
Write-Host "Teams Process Sucessfully Stopped" -ForegroundColor Green | |
LogWrite "Teams Process Sucessfully Stopped" | |
}catch{ | |
#echo $_ | |
LogWrite "Unable to stop Teams Process" | |
LogWrite $_.Exception.Message | |
} | |
Write-Host "Stopping Chrome Process" -ForegroundColor Yellow | |
LogWrite "Stopping Chrome Process" | |
try{ | |
Get-Process -ProcessName Chrome| Stop-Process -Force | |
Start-Sleep -Seconds 3 | |
Write-Host "Chrome Process Sucessfully Stopped" -ForegroundColor Green | |
LogWrite "Chrome Process Sucessfully Stopped" | |
}catch{ | |
#echo $_ | |
LogWrite "Unable to stop Chrome Process" | |
LogWrite $_.Exception.Message | |
} | |
Write-Host "Clearing Chrome Cache" -ForegroundColor Yellow | |
LogWrite "Clearing Chrome Cache" | |
try{ | |
Get-ChildItem -Path $env:LOCALAPPDATA"\Google\Chrome\User Data\Default\Cache" | Remove-Item -Confirm:$false | |
Get-ChildItem -Path $env:LOCALAPPDATA"\Google\Chrome\User Data\Default\Cookies" -File | Remove-Item -Confirm:$false | |
Get-ChildItem -Path $env:LOCALAPPDATA"\Google\Chrome\User Data\Default\Web Data" -File | Remove-Item -Confirm:$false | |
Write-Host "Chrome Cleaned" -ForegroundColor Green | |
LogWrite "Chrome Cleaned" | |
}catch{ | |
#echo $_ | |
LogWrite "Unable to clear Chrome cache" | |
LogWrite $_.Exception.Message | |
} | |
Write-Host "Stopping IE Process" -ForegroundColor Yellow | |
LogWrite "Stopping IE Process" | |
try{ | |
Get-Process -ProcessName MicrosoftEdge | Stop-Process -Force | |
Get-Process -ProcessName IExplore | Stop-Process -Force | |
Write-Host "Internet Explorer and Edge Processes Sucessfully Stopped" -ForegroundColor Green | |
LogWrite "Internet Explorer and Edge Processes Sucessfully Stopped" | |
}catch{ | |
#echo $_ | |
Logwrite "Unable to stop Internet Explorer and Edge Processes" | |
LogWrite $_.Exception.Message | |
} | |
Write-Host "Clearing IE Cache" -ForegroundColor Yellow | |
LogWrite "Clearing IE / Edge Cache" | |
try{ | |
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 8 | |
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 2 | |
Write-Host "IE and Edge Cleaned" -ForegroundColor Green | |
LogWrite "IE / Edge cache cleaned" | |
}catch{ | |
#echo $_ | |
LogWrite "Unable to clear IE/Edge cache" | |
LogWrite $_.Exception.Message | |
} | |
Write-Host "Cleanup steps completed..." -ForegroundColor Green | |
LogWrite "Cleanup steps completed..." | |
} | |
if ($uninstall -eq "Y"){ | |
Write-Host "STARTING Uninstall MS Teams Script" -ForegroundColor Green | |
LogWrite "STARTING Uninstall MS Teams Script" | |
# Removal Machine-Wide Installer - This needs to be done before removing the EXE! | |
Try { | |
Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "Teams Machine-wide Installer"} | Remove-WmiObject | |
} Catch { | |
#echo $_ | |
LogWrite "Teams Machine-Wide Installer not found." | |
LogWrite "** ENDING Uninstall MS Teams Script **" | |
exit | |
} | |
#Variables | |
if (Test-Path "$env:LOCALAPPDATA\Microsoft\Teams") { | |
LogWrite "Teams found for user, uninstalling..." | |
try { | |
Start-Process -FilePath "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe" -ArgumentList "-uninstall -s" -EA Stop|out-null | |
}Catch { | |
LogWrite "Teams app Uninstall for user Failed! Error Message:" | |
LogWrite $_.Exception.Message | |
Out-Null | |
} | |
Remove-Item -recurse "$env:LOCALAPPDATA\Microsoft\Teams\" | |
LogWrite "** ENDING Uninstall MS Teams Script **" | |
} | |
} | |
if ($reinstall -eq "Y"){ | |
if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*") | |
{ | |
#64bit code here | |
Write-Host "64-bit OS" | |
LogWrite "64-bit OS" | |
$source = "https://go.microsoft.com/fwlink/p/?LinkID=869426&culture=fr-fr&country=WW&lm=deeplink&lmsrc=groupChatMarketingPageWeb&cmpid=directDownloadWin64"; | |
} | |
else | |
{ | |
#32bit code here | |
Write-Host "32-bit OS" | |
LogWrite "32-bit OS" | |
} | |
$source = "https://go.microsoft.com/fwlink/p/?LinkID=869426&culture=fr-fr&country=WW&lm=deeplink&lmsrc=groupChatMarketingPageWeb&cmpid=directDownloadWin64"; | |
$destination = "c:\Temp\Teams\Teams.exe" | |
if ($uptodate -eq "Y"){ | |
LogWrite "teams package is downloading from microsoft website to $destination" | |
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" | |
Invoke-WebRequest $source -OutFile $destination | |
} | |
#If installation file exist, install teams | |
if (Test-Path $destination) { | |
LogWrite "Teams installation in progress" | |
try { | |
Start-Process -FilePath $destination -ArgumentList " -s" |out-null | |
LogWrite "Teams installation completed" | |
Start-Process -File "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe" -ArgumentList '--processStart "Teams.exe"'|out-null | |
}catch{ | |
Logwrite $_.Exception.Message | |
LogWrite "Teams installation failed" | |
} | |
if ($deleteInstall -eq "Y"){ | |
#Delete installer | |
Remove-Item -recurse $destination | |
LogWrite "Teams installation file deleted" | |
} | |
}else{ | |
Logwrite "$destination not found in your system. Please try to download from website" | |
} | |
#install software | |
Invoke-item $Logfile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment