Created
March 11, 2021 16:38
-
-
Save Smalls1652/e3c3a1457a53e63eb13e3a8a9924b62e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[CmdletBinding(SupportsShouldProcess)] | |
param( | |
) | |
#The download URL for the Teams Machine-Wide Installer for x64 systems. | |
$teamsInstallDownloadUri = "https://teams.microsoft.com/downloads/desktopurl?env=production&plat=windows&arch=x64&managedInstaller=true&download=true" | |
# -- Begin searching for a current install of Teams -- | |
Write-Verbose "Looking for a current installation of Teams Machine-Wide Installer." | |
$uninstallRegKeyPaths = @( | |
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", | |
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | |
) | |
#Loop through both of the 'Uninstall' keys and get their data. | |
$uninstallRegKeys = [System.Collections.Generic.List[pscustomobject]]::new() | |
foreach ($uninstallRegKeyPath in $uninstallRegKeyPaths) { | |
$regKeys = Get-ChildItem -Path $uninstallRegKeyPath | |
foreach ($regKey in $regKeys) { | |
$keyData = Get-ItemProperty -Path $regKey.PSPath | |
$uninstallRegKeys.Add( | |
[pscustomobject]@{ | |
"Name" = $regKey.PSChildName; | |
"Path" = $regKey.PSPath; | |
"KeyData" = $keyData; | |
} | |
) | |
} | |
} | |
#Filter the registry keys found for "Teams Machine-Wide Installer" | |
$msTeamsUninstallKey = $uninstallRegKeys | Where-Object { $PSItem.KeyData.DisplayName -eq "Teams Machine-Wide Installer" } | |
switch ($null -eq $msTeamsUninstallKey) { | |
$false { | |
#If a key was found, start the uninstall process. | |
Write-Warning "Teams Machine-Wide Installer registry key was found." | |
if ($PSCmdlet.ShouldProcess("Teams Machine-Wide Installer", "Uninstall")) { | |
Start-Process -FilePath "msiexec" -ArgumentList @("/x" , "$($msTeamsUninstallKey.Name)", "/qn") -NoNewWindow -Wait | |
} | |
break | |
} | |
#If no key was found, continue on and try the install. | |
} | |
#Build a path for temporarily saving the installer. | |
$tempDir = [System.IO.Path]::GetTempPath() | |
$downloadPath = Join-Path -Path $tempDir -ChildPath "Teams_windows_x64.msi" | |
#Download the Teams Machine-Wide Installer | |
if ($PSCmdlet.ShouldProcess("Teams_windows_x64.msi", "Download MSI to $($tempDir)")) { | |
$ProgressPreference = "SilentlyContinue" #Disable the progress UI from showing. This improves download performance. | |
Invoke-WebRequest -Uri $teamsInstallDownloadUri -OutFile $downloadPath -ErrorAction "Stop" | |
$ProgressPreference = "Continue" | |
} | |
#Install Teams Machine-Wide Installer | |
if ($PSCmdlet.ShouldProcess("Teams Machine-Wide Installer", "Install")) { | |
Start-Process -FilePath "msiexec" -ArgumentList @("/i", "`"$($downloadPath)`"", "/qn", "OPTIONS=`"noAutoStart=true`"", "ALLUSERS=1") -Wait -NoNewWindow | |
} | |
#Cleanup the temp file | |
if ($PSCmdlet.ShouldProcess($downloadPath, "Remove install file")) { | |
Remove-Item -Path $downloadPath -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment