Shared Script Library
GitHubLinkedInSponsor
  • Overview
  • Software
    • Installers
      • Bluebeam Revu
      • Browser Extension
      • Google Chrome
      • Microsoft Defender for Endpoint
      • Microsoft Office
      • Microsoft Teams
      • QuickBooks Desktop
    • Uninstallers
      • Browser Extension
  • Device Management
    • Backups
      • SQL Databases
      • Synology Active Backup
      • Windows Restore Point
    • Updates
      • Dell Command Update
      • Microsoft Office
      • Microsoft Store Applications
      • Windows Management Framework
      • Windows Updates
    • Misc
      • Reset Local Group Policy
Powered by GitBook
On this page
  • Overview
  • Script
  • Examples
  • Example 1
  • Example 2
  • Example 3
  • Example 4
  • Parameters

Was this helpful?

  1. Software
  2. Installers

Microsoft Office

PowerShell script to cleanly install Microsoft Office.

PreviousMicrosoft Defender for EndpointNextMicrosoft Teams

Last updated 1 month ago

Was this helpful?

Overview

This script uses the Office Deployment Tool (ODT) to perform a clean install of Microsoft Office with a configuration xml file. For convenience, it includes a built-in xml file that will install Microsoft Office 365 Apps for Business if a configuration file is not specified.

Prerequisites:

  1. Store the configuration xml in a location that will be accessible to the script at runtime (either file path or url).

  2. Notate the path / url of the configuration xml. If using GitHub to host the file, be sure to get the raw URL.

Notes:

  • When using the built-in configuration xml, the 64-bit version of Microsoft Office 365 will be installed, unless a 32-bit OS is detected or the -x86 switch is used.

  • This script will uninstall the Microsoft Office Hub Microsoft Store application.


Script

Script will remove existing installations of Microsoft Office when used with the default configuration file.

Examples

Example 1

.\InstallOffice.ps1

This example installs Microsoft Office 365 Apps for Business using the built-in XML configuration file. All existing versions of Microsoft Office will be removed, including the Microsoft Office Hub Microsoft Store application.

Example 2

.\InstallOffice.ps1 -x86

This example installs the 32-bit version of Microsoft Office 365 Apps for Business using the built-in XML configuration file. All existing versions of Microsoft Office will be removed, including the Microsoft Office Hub Microsoft Store application.

Example 3

.\InstallOffice.ps1 -Config "C:\temp\office-config.xml"

This example utilizes the provided XML configuration file to install Microsoft Office.

Example 4

.\InstallOffice.ps1 -Config "https://raw.githubusercontent.com/wise-io/scripts/office-xmls/config.xml"

This example downloads the XML configuration file from the provided URL and installs Microsoft Office.


Parameters

-Config

Optional string parameter that allows you to provide a file path or URL to an office configuration xml. This XML will be used to determine what Microsoft Office products to install or remove.

-x86

Aliases: -32, -32bit

Optional switch parameter that allows the installation of the 32-bit version of Microsoft Office 365, even on 64-bit systems.

Note: This parameter cannot be used with -Config.

If necessary, create a configuration xml at .

https://config.office.com
Overview of the Office Deployment Tool - Deploy OfficeMicrosoftLearn
Logo
https://github.com/wise-io/scripts/blob/main/scripts/InstallOffice.ps1
<#
  .SYNOPSIS
    Installs Microsoft Office 365
  .DESCRIPTION
    Installs Microsoft Office 365 using a default configuration xml, unless a custom xml is provided.
    WARNING: This script will remove all existing office installations if used with the default configuration xml.
  .PARAMETER Config
    Parameter Set: Custom
    URL or file path to custom configuration xml for office installations.
  .PARAMETER x86
    Parameter Set: Builtin
    Switch parameter to install 32-bit Office applications with the built-in XML.
  .LINK
    XML Configuration Generator: https://config.office.com/
  .NOTES
    Author: Aaron J. Stevenson
#>

[CmdletBinding(DefaultParameterSetName = 'None')]
param (
  [Parameter(ParameterSetName = 'Custom')]
  [Alias('Configure')][String]$Config,

  [Parameter(ParameterSetName = 'Builtin')]
  [Alias('32', '32bit')][Switch]$x86
)

function Test-ValidUrl {
  param([String]$Url)

  try {
    $Uri = [System.Uri]::New($Url)
    return $Uri.Scheme -in @('http', 'https')
  }
  catch {
    return $false
  }
}

function Get-ODT {
  [String]$MSWebPage = Invoke-RestMethod 'https://www.microsoft.com/en-us/download/details.aspx?id=49117'
  $Script:ODTURL = $MSWebPage | ForEach-Object {
    if ($_ -match '.*href="(https://download.microsoft.com.*officedeploymenttool.*\.exe)"') { $Matches[1] }
  }

  try {
    Write-Output "`nDownloading Office Deployment Tool (ODT)..."
    Invoke-WebRequest -Uri $Script:ODTURL -OutFile $Script:Installer
    Start-Process -Wait -NoNewWindow -FilePath $Script:Installer -ArgumentList "/extract:$Script:ODT /quiet"
  }
  catch {
    Remove-Item $Script:ODT, $Script:Installer -Recurse -Force -ErrorAction Ignore
    Write-Warning 'There was an error downloading the Office Deployment Tool.'
    Write-Warning $_
    exit 1
  }
}

function Set-ConfigXML {
  # Create path to config file
  $Path = Split-Path -Path $Script:ConfigFile -Parent
  if (!(Test-Path -Path $Path -PathType Container)) {
    New-Item -Path $Path -ItemType Directory | Out-Null
  }

  # Determine type of config provided
  switch ($Config) {
    { ($_) -and (Test-Path -Path $_ -PathType Leaf -Include '*.xml') } { $ConfigPath = $true }
    { ($_) -and (Test-ValidUrl -Url $_) } { $ConfigUrl = $true }
    default { $DefaultConfig = $true }
  }

  # If path provided, copy file to temp directory
  if ($ConfigPath) {
    Write-Output 'Configuration file path provided - copying file to temp directory for installation...'
    try { Copy-Item -Path $Config -Destination $Script:ConfigFile }
    catch {
      Write-Warning 'Unable to copy configuration file'
      Write-Warning $_
      exit 1
    }
  }

  # If url provided, download file to temp directory
  if ($ConfigUrl) {
    Write-Output 'Configuration url provided - downloading file to temp directory for installation...'
    try { Invoke-WebRequest -Uri $Config -OutFile $Script:ConfigFile }
    catch {
      Write-Warning 'Unable to download configuration file'
      Write-Warning $_
      exit 1
    }
  }

  # If no configuration provided, create default configuration file in temp directory
  if ($DefaultConfig) {
    Write-Output 'No configuration file provided - creating default configuration file for installation...'
    try {
      $XML = [XML]@'
  <Configuration ID="0fb449fc-f210-4428-9c7d-be5882ab97aa">
    <Remove All="TRUE"/>
    <Add OfficeClientEdition="64" Channel="MonthlyEnterprise" MigrateArch="TRUE">
      <Product ID="O365ProPlusRetail">
        <Language ID="MatchOS" />
        <ExcludeApp ID="Groove" />
        <ExcludeApp ID="Lync" />
        <ExcludeApp ID="Bing" />
      </Product>
    </Add>
    <Property Name="SharedComputerLicensing" Value="0" />
    <Property Name="FORCEAPPSHUTDOWN" Value="TRUE" />
    <Property Name="DeviceBasedLicensing" Value="0" />
    <Property Name="SCLCacheOverride" Value="0" />
    <Updates Enabled="TRUE" />
    <RemoveMSI />
    <AppSettings>
      <User Key="software\microsoft\office\16.0\excel\options" Name="defaultformat" Value="51" Type="REG_DWORD" App="excel16" Id="L_SaveExcelfilesas" />
      <User Key="software\microsoft\office\16.0\powerpoint\options" Name="defaultformat" Value="27" Type="REG_DWORD" App="ppt16" Id="L_SavePowerPointfilesas" />
      <User Key="software\microsoft\office\16.0\word\options" Name="defaultformat" Value="" Type="REG_SZ" App="word16" Id="L_SaveWordfilesas" />
    </AppSettings>
    <Display Level="Full" AcceptEULA="TRUE" />
  </Configuration>
'@

      if ($x86 -or !([Environment]::Is64BitOperatingSystem)) {
        $OfficeClientEdition = $XML.SelectSingleNode('//Add[@OfficeClientEdition]')
        $OfficeClientEdition.SetAttribute('OfficeClientEdition', '32')
      }
    
      $XML.Save("$Script:ConfigFile")
    }
    catch { 
      Write-Warning 'Unable to create default configuration file'
      Write-Warning $_
      exit 1
    }
  }
}

function Install-Office {
  Write-Output 'Installing Microsoft Office...'
  try { 
    Start-Process -Wait -WindowStyle Hidden -FilePath "$Script:ODT\setup.exe" -ArgumentList "/configure $Script:ConfigFile"
    Write-Output 'Installation complete.'
  }
  catch {
    Write-Warning 'Error during Office installation:'
    Write-Warning $_
  }
  finally { Remove-Item $Script:ODT, $Script:Installer -Recurse -Force -ErrorAction Ignore }
}

function Remove-OfficeHub {
  $AppName = 'Microsoft.MicrosoftOfficeHub'
  try {
    $Package = Get-AppxPackage -AllUsers | Where-Object { ($AppName -contains $_.Name) }
    $ProvisionedPackage = Get-AppxProvisionedPackage -Online | Where-Object { ($AppName -contains $_.DisplayName) }
    if ($Package -or $ProvisionedPackage) {
      Write-Output "`nRemoving [$AppName] (Microsoft Store App)..."
      $ProvisionedPackage | Remove-AppxProvisionedPackage -AllUsers | Out-Null
      $Package | Remove-AppxPackage -AllUsers
    }
  }
  catch { 
    Write-Warning "Error during [$AppName] removal:"
    Write-Warning $_
  }
}

$Script:ODT = "$env:temp\ODT"
$Script:ConfigFile = "$Script:ODT\office-config.xml"
$Script:Installer = "$env:temp\ODTSetup.exe"

# Adjust PowerShell settings
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12' -and [Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls13') {
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}

Get-ODT 
Set-ConfigXML
Install-Office
Remove-OfficeHub