Need Help figuring out error in script

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>

Make sure $MachineWide is not null, it looks like no product is installed with name "Teams Machine-Wide Installer"

Note: Its not recommended to use win32_product class to get the products installed, its slower as well.

The best approach is to read the Uninstall regkey for getting installed products.

Gathering Installed Software Using PowerShell – Microsoft Certified Professional Magazine Online (mcpmag.com)

The Teams user intallation and the Teams Machine-wide Installer are two actually unrelated installations!! The Teams app a user can use is actually by default a user installation inside the user profile. So even when you’ve uninstalled the Teams Machine-wide Installer the user is still able to run the Teams app from its profile. :wink:

Olaf, I whole heartedly agree with that assessment, What I am trying to do is loop through each profile on the devices and perform the uninstall from them as well. Is that even possible? Maybe I am barking up the wrong tree here. At the end of the day I thought PowerShell could help me remove teams on all the devices in my environment that still have it installed and I pulled that script together from bits and pieces of other scripts and command-lets but to no avail. So I thought I would ask for insight on how to get PowerShell to do what I need to accomplish. Any insight on how this can be accomplished is welcomed!

Probably. But I’d recommend to use a technique called Active Setup. Here you have a short explanation what it is:

And here you have a short explanation how it works:

So instead of trying to uninstall all Teams-Installations of all users of a PC at once you uninstall the Teams of the user logging on at the moment he or she loggs on. Of course you should have uninstalled the Machine-wide Installer before. :wink: