Good Morning,
I am trying to run this script to uninstall Microsoft Teams from about 150 windows 10 machines. It appears to remove teams but always throws an error while doing so. I am trying to figure out why it is throwing the error and how I can fix it so it does not throw the error.
Below is the code being run and below that the error it is throwing. Any insight on why this is occurring would be helpful and if you can point me in the right direction on how to prevent it that would be greatly appreciated also.
Script
function unInstallTeams($path) {
$clientInstaller = "$($path)\Update.exe"
try {
$process = Start-Process -FilePath "$clientInstaller" -ArgumentList "--uninstall /s" -PassThru -Wait -ErrorAction STOP
if ($process.ExitCode -ne 0)
{
Write-Error "UnInstallation failed with exit code $($process.ExitCode)."
}
}
catch {
Write-Error $_.Exception.Message
}
}
# Remove Teams Machine-Wide Installer
Write-Host "Removing Teams Machine-wide Installer" -ForegroundColor Yellow
$MachineWide = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq "Teams Machine-Wide Installer"}
$MachineWide.Uninstall()
# Remove Teams for Current Users
$localAppData = "$($env:LOCALAPPDATA)\Microsoft\Teams"
$programData = "$($env:ProgramData)\$($env:USERNAME)\Microsoft\Teams"
If (Test-Path "$($localAppData)\Current\Teams.exe")
{
unInstallTeams($localAppData)
}
elseif (Test-Path "$($programData)\Current\Teams.exe") {
unInstallTeams($programData)
}
else {
Write-Warning "Teams installation not found"
}
Error thrown:
PS C:\Windows\system32> C:\Scripts\uninstall_teams.ps1
Removing Teams Machine-wide Installer
You cannot call a method on a null-valued expression.
At C:\Scripts\uninstall_teams.ps1:19 char:1
+ $MachineWide.Uninstall()
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
WARNING: Teams installation not found
PS C:\Windows\system32>