TeamsOnWVD.psm1

########## Teams for Windows Virtual Desktop
# Author : Yannick Dils
# Create Date : 09/04/2020
# Version : 2.0
##########

function install-teams64bit
{
#region step 1 - Import regkeys

Write-Host "Step 1 : Checking the required registry keyssn" -ForegroundColor Cyan


$KeyPath = "HKLM:\SOFTWARE\Microsoft\Teams\"
$WVDKey = "IsWVDEnvironment"

        IF(!(Test-Path $KeyPath))
            {
                Write-Host "The path does not exist, we will create this path now" -ForegroundColor red
                New-Item -Path $KeyPath -Force 
                New-ItemProperty -Path $KeyPath -Name $WVDKey -PropertyType "DWORD" -Value "1" -Force
            }
        else
            {
                Write-Host "The path already exists, we don't need to create this path now" -ForegroundColor Green
                New-ItemProperty -Path $KeyPath -Name $WVDKey -PropertyType "DWORD" -Value "1" -Force 
            }
#endregion step 1
#region step 2 - Download the Teams MSI Package


$DownloadLink = "https://statics.teams.cdn.office.net/production-windows-x64/1.3.00.4461/Teams_windows_x64.msi"
$TempDir = "C:\Temp"
$Output = "C:\Temp\teams64.msi"
$Logfile = $TempDir + "\logfile.txt"
$Path = Test-Path $TempDir

    If ($Path -eq $true)
        {
            Write-Host "Step 2 : The C:\Temp path already exists, no need to create one" -ForegroundColor Cyan
        }
    Else
        {
            Write-host "Step 2 : We are creating a temp directory C:\Temp" -ForegroundColor Cyan
            $DontShow = mkdir $TempDir
        }

(New-Object System.Net.WebClient).DownloadFile($DownloadLink, $output)
cd $TempDir
#endregion step 2
#region step 3 - Install Teams MSI

Write-Host "Step 3 : Let us install Teams" -ForegroundColor Cyan
msiexec /i $Output /l*v $Logfile ALLUSER=1 ALLUSERS=1

#endregion step 3
}

function install-teams32bit
{
#region step 1 - import regkeys

Write-Host "Step 1 : Let us first set a couple or registry keysn" -ForegroundColor Cyan

$KeyPath = "HKLM:\SOFTWARE\Microsoft\Teams\"
$WVDKey = "IsWVDEnvironment"

        IF(!(Test-Path $KeyPath))
            {
                Write-Host "The path does not exist, we will create this path now" -ForegroundColor red
                New-Item -Path $KeyPath -Force 
                New-ItemProperty -Path $KeyPath -Name $WVDKey -PropertyType "DWORD" -Value "1" -Force

            }
        else
            {
                Write-Host "The path already exists, we don't need to create this path now" -ForegroundColor Green
                New-ItemProperty -Path $KeyPath -Name "$WVDKey" -PropertyType "DWORD" -Value "1" -Force 
            }
#endregion step 1
#region step 2 - Download the Teams MSI Package


$DownloadLink = "https://statics.teams.cdn.office.net/production-windows/1.3.00.4461/Teams_windows.msi"
$TempDir = "C:\Temp"
$Output = "C:\Temp\teams32.msi"
$Logfile = $TempDir + "\logfile.txt"
$Path = Test-Path $TempDir

    If ($Path -eq $true)
        {
            Write-Host "Step 2 : The C:\Temp path already exists, no need to create one" -ForegroundColor Cyan
        }
    Else
        {
            Write-host "Step 2 : We are creating a temp directory C:\Temp" -ForegroundColor Cyan
            $DontShow = mkdir $TempDir
        }

(New-Object System.Net.WebClient).DownloadFile($DownloadLink, $output)
cd $TempDir
#endregion step 2
#region step 3 - Install Teams MSI

Write-Host "Step 3 : Let us install Teams" -ForegroundColor Cyan
msiexec /i $Output /l*v $Logfile ALLUSER=1 ALLUSERS=1

#endregion step 3
}

function remove-teams64bit
{
#region step 1 - Teams Per User Uninstall

Write-Host "Step 1 : Let us un-install Teams per user base" -ForegroundColor Cyan

Get-WmiObject -Class Win32_Product | Where-Object {$_.IdentifyingNumber -eq "{39AF0813-FA7B-4860-ADBE-93B9B214B914}"} | Remove-WmiObject
$ListUsers = Get-ChildItem -Path "$($ENV:SystemDrive)\Users"

$ListUsers | ForEach-Object {
    Try { 
        if (Test-Path "$($ENV:SystemDrive)\Users\$($_.Name)\AppData\Local\Microsoft\Teams") {
            Start-Process -FilePath "$($ENV:SystemDrive)\Users\$($_.Name)\AppData\Local\Microsoft\Teams\Update.exe" -ArgumentList "-uninstall -s"
        }
    } Catch { 
        Out-Null
    }
}

$ListUsers | ForEach-Object {
    Try {
        if (Test-Path "$($ENV:SystemDrive)\Users\$($_.Name)\AppData\Local\Microsoft\Teams") {
            Remove-Item â€“Path "$($ENV:SystemDrive)\Users\$($_.Name)\AppData\Local\Microsoft\Teams" -Recurse -Force -ErrorAction Ignore
        }
    } Catch {
        Out-Null
    }
}
#endregion step 1
#region step 2 - Teams Machine Wide Uninstall
Write-Host "Step 2 : Let us un-install Teams per machine base" -ForegroundColor Cyan

#get-wmiobject Win32_Product | Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize
$TeamsWMI = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "Teams Machine-Wide Installer"} 
$TeamsGUID = $TeamsWMI.IdentifyingNumber
MsiExec.exe /X$TeamsGUID /qn 
$TeamsWMI | Remove-WmiObject
#endregion step 2 - Teams Machine Wide Uninstall
#region step 3 - Remove regkeys

Write-Host "Step 3 : Checking the required registry keyssn" -ForegroundColor Cyan


$KeyPath = "HKLM:\SOFTWARE\Microsoft\Teams\"
$WVDKey = "IsWVDEnvironment"

        IF(!(Test-Path $KeyPath))
            {
                Write-Host "The path does not exist, we don't have to remove it" -ForegroundColor red
                
            }
        else
            {
                Write-Host "The path exists, we will remove it now" -ForegroundColor Green
                Remove-Item -Path $KeyPath -Force 
                #Remove-ItemProperty -Path $KeyPath -Name $WVDKey -PropertyType "DWORD" -Value "1" -Force
            }
#endregion step 3


}