Uninstall old versions of Office/Install newer version

Greetings all,

Been toying around with PSADT more and more and I’ve been tasked with creating a package to remove Office 2003-2010 (yes, we still have users running these sadly), and then replacing it with Office 2016.

I used the Microsoft Office scrubbers as part of the pre-install and only have these three versions defined, and it actually worked quite well. The problem I am running into is if they have both an old version AND 2016 installed.

It removes the previous version as expected, but then tries to remove 2016 as well…even though I dont have it defined in the pre-install.

I’ll post my code here if anyone has any ideas?

##===============================================
##
PRE-INSTALLATION
##*===============================================
[string]$installPhase = ‘Pre-Installation’

Show Welcome Message, close Internet Explorer if required, allow up to 3 deferrals, verify there is enough disk space to complete the install, and persist the prompt

Show-InstallationWelcome -CloseApps “iexplore,PWConsole,excel,groove,infopath,onenote,outlook,mspub,powerpnt,winword,communicator,skype,lync,msaccess” -BlockExecution -CheckDiskSpace -PersistPrompt -CloseAppsCountdown 300

Display Pre-Install cleanup status

    Show-InstallationProgress "Installing Office 2016. Performing Pre-Install cleanup. This may take some time. Please wait..."
    # Remove any previous version of Office (if required)
    $officeExecutables = @("excel.exe", "groove.exe", "onenote.exe", "infopath.exe", "onenote.exe", "outlook.exe", "mspub.exe", "powerpnt.exe", "winword.exe")
    ForEach ($officeExecutable in $officeExecutables) {
    If (Test-Path (Join-Path $dirOffice "Office11\$officeExecutable")) {
    Write-Log "Microsoft Office 2003 was detected. Will be uninstalled."
    Execute-Process -FilePath "CScript.Exe" -Arguments "`"$dirSupportFiles\OffScrub03.vbs`" STANDARD,PROPLUS,PROOFKIT /S /Q /NoCancel" -WindowStyle Hidden -IgnoreExitCodes "1,2,3"
    Break
    }
    }
    ForEach ($officeExecutable in $officeExecutables) {
    If (Test-Path (Join-Path $dirOffice "Office12\$officeExecutable")) {
    Write-Log "Microsoft Office 2007 was detected. Will be uninstalled."
    Execute-Process -FilePath "CScript.Exe" -Arguments "`"$dirSupportFiles\OffScrub07.vbs`" STANDARD,PROPLUS,PROOFKIT /S /Q /NoCancel" -WindowStyle Hidden -IgnoreExitCodes "1,2,3"
    Break
    }
    }
    ForEach ($officeExecutable in $officeExecutables) {
    If (Test-Path (Join-Path $dirOffice "Office14\$officeExecutable")) {
    Write-Log "Microsoft Office 2010 was detected. Will be uninstalled."
    Execute-Process -FilePath "CScript.Exe" -Arguments "`"$dirSupportFiles\OffScrub10.vbs`" STANDARD,PROPLUS,PROOFKIT /S /Q /NoCancel" -WindowStyle Hidden -IgnoreExitCodes "1,2,3"
    Break
    }
    }
    }

##===============================================
##
INSTALLATION
##*===============================================
[string]$installPhase = ‘Installation’

Handle Zero-Config MSI Installations

If ($useDefaultMsi) {
[hashtable]$ExecuteDefaultMSISplat = @{ Action = ‘Install’; Path = $defaultMsiFile }; If ($defaultMstFile) { $ExecuteDefaultMSISplat.Add(‘Transform’, $defaultMstFile) }
Execute-MSI @ExecuteDefaultMSISplat; If ($defaultMspFiles) { defaultMspFiles | ForEach-Object { Execute-MSI -Action 'Patch' -Path _ } }
}

    # Installing Office 2016 Pro Plus
    Show-InstallationProgress "Installing Microsoft Office 2016. This may take some time. Please wait..."
    Execute-Process -FilePath "$dirFiles\32bit\setup.exe" -Arguments " /adminfile `"$dirFiles\32bit\MyFile.MSP`"" -WindowStyle Hidden -IgnoreExitCodes "3010,1073807364" -ContinueOnError $true -WaitForMsiExec