Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mark05e/7a5ca5af93a588718aaf084b58d21493 to your computer and use it in GitHub Desktop.
Save mark05e/7a5ca5af93a588718aaf084b58d21493 to your computer and use it in GitHub Desktop.
[CmdletBinding(SupportsShouldProcess=$True)]
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-Sleep 5
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