Saturday, 11 March 2023

PowerShell Script: Uninstall Microsoft Teams from a workstation

This script will uninstall Microsoft Teams from a workstation, it will also work while running under a different user, in case you need to role it out via a RMM solution but would work as a Group policy too

# Check if script is running with administrative privileges
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "This script must be run with administrative privileges."
    Exit 1001
}

# Uninstall Microsoft Teams for all users
$teams_installed = Get-ChildItem -Path "C:\Users\*\AppData\Local\Microsoft\Teams\Update.exe" -ErrorAction SilentlyContinue

if ($teams_installed) {
    # Loop through all instances of Microsoft Teams and uninstall it silently
    foreach ($teams in $teams_installed) {
        Start-Process "$($teams.FullName)" -ArgumentList "-uninstall -s" -Wait
    }
    Write-Host "Microsoft Teams has been uninstalled for all users."
} else {
    Write-Host "Microsoft Teams is not installed on this computer."
}

# Uninstall Teams machine-wide installer
$teamswide_installed = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -like "*Teams Machine-Wide*"} | Select-Object -ExpandProperty IdentifyingNumber

if ($teamswide_installed) {
    # Uninstall the Teams machine-wide installer silently
    Start-Process "msiexec.exe" -ArgumentList "/x $teamswide_installed /qn" -Wait
    Write-Host "Teams machine-wide installer has been uninstalled."
} else {
    Write-Host "Teams machine-wide installer is not installed on this computer."
}

No comments:

Post a Comment