# Need Help figuring out error in script

**URL:** <https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993>\
**Category:** PowerShell Help\
**Created:** [July 6, 2021, 2:36pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993 "2021-07-06T14:36:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![orlandot](https://avatars.discourse-cdn.com/v4/letter/o/3ec8ea/32.png) [@orlandot](https://forums.powershell.org/u/orlandot)\
**Post date:** [July 6, 2021, 2:36pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993/1 "2021-07-06T14:36:43Z")

</div>

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

```auto
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:

```auto
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>

```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [July 6, 2021, 2:46pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993/2 "2021-07-06T14:46:26Z")

</div>

> [@orlandot](#):
>
> `$MachineWide`

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)](https://mcpmag.com/articles/2017/07/27/gathering-installed-software-using-powershell.aspx#:~:text=The%20alternative%20to%20this%20is,Microsoft%5CWindows%5CCurrentVersion%5CUninstall)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 6, 2021, 2:56pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993/3 "2021-07-06T14:56:17Z")

</div>

> [@orlandot](#):
>
> I am trying to run this script to uninstall Microsoft Teams from about 150 windows 10 machines.

> [@orlandot](#):
>
> `Removing Teams Machine-wide Installer`

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. 😉

---

<div class="post-metadata">

**Author:** ![orlandot](https://avatars.discourse-cdn.com/v4/letter/o/3ec8ea/32.png) [@orlandot](https://forums.powershell.org/u/orlandot)\
**Post date:** [July 7, 2021, 6:47pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993/4 "2021-07-07T18:47:25Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 7, 2021, 8:32pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993/5 "2021-07-07T20:32:38Z")

</div>

> [@orlandot](#):
>
> Is that even possible?

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

> **[Active Setup](https://en.wikipedia.org/wiki/Active_Setup)**
>
> Active Setup is a mechanism for executing commands once per user early during login. Active Setup is used by some Microsoft Windows operating system components like Internet Explorer to set up an initial configuration for new users logging on for the first time. Active Setup is also used in some corporations’ software distribution systems to create an initial customized user environment.
> Active Setup uses both machine-specific data and user-specific data. The machine part consists of a list of c...

And here you have a short explanation how it works:

> **[Active Setup Explained • Helge Klein](https://helgeklein.com/blog/active-setup-explained/)**
>
> Tools & knowledge for IT pros

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. 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:23pm UTC](https://forums.powershell.org/t/need-help-figuring-out-error-in-script/16993/6 "2024-05-16T20:23:58Z")

</div>


