Get-OfficeVersion

PowerShell function that gets the currently installed office version and returns either the year, the office folder, the registry location, or the number.

Updated 4/25/18 to support 64 bit office. Bitness is now a returnable Value Type.


Function Get-OfficeVersion {
#Allowing Cmdlet Bindings
[CmdletBinding(SupportsShouldProcess=$true)]
param(
  [Parameter(Position=0,Mandatory=$true)]
  [Alias("Value","VT")]
  [ValidateSet("Office Year", "Office Folder", "Registry Location","Registry Number","Bitness")]
  [string]$ValueTypeReturn,
    [Parameter(Position=1,Mandatory=$false)]
  [Alias("Computer","CN")]
  [string]$ComputerName = $env:COMPUTERNAME
)
Write-Verbose "Testing connection to $ComputerName"
if(Test-Connection -Quiet $ComputerName -Count 1){
Write-Verbose "Connection Successful to $ComputerName"
Write-Verbose "Detecting software for $ComputerName"
    if ((Test-path "\\$ComputerName\c$\Program Files (x86)\Microsoft Office\") -eq $true)
    {
        Write-Verbose "Found existing 32bit Office files on $ComputerName."
        if ((Test-path "\\$ComputerName\c$\Program Files (x86)\Microsoft Office\root\Office16\LIBRARY\") -eq $true)
        {
            Write-Verbose "Found 32bit Office 2016"
            $OfficeYear = "2016"           
            $ProgramFilesLocation = "C:\Program Files (x86)"
            $Bitness = "x32"
        }
        elseif ((Test-path "\\$ComputerName\c$\Program Files\Microsoft Office\root\Office16\LIBRARY\") -eq $true)
        {
            Write-Verbose "Found 64bit Office 2016"
            $ProgramFilesLocation = "C:\Program Files"
            $Bitness = "x64"
            $OfficeYear = "2016"   
        }
        if ((Test-path "\\$ComputerName\c$\Program Files (x86)\Microsoft Office\Office14\LIBRARY\") -eq $true)
        {
            Write-Verbose "Found 32bit Office 2010"
            $ProgramFilesLocation = "C:\Program Files (x86)"
            $OfficeYear = "2010"
            $Bitness = "x32"
        }
        elseif ((Test-path "\\$ComputerName\c$\Program Files\Microsoft Office\Office14\LIBRARY\") -eq $true)
        {
            Write-Verbose "Found 64bit Office 2010"
            $ProgramFilesLocation = "C:\Program Files"
            $OfficeYear = "2010"
            $Bitness = "x64"   
        }
        if ((Test-path "\\$ComputerName\c$\Program Files (x86)\Microsoft Office\Office15\LIBRARY\") -eq $true)
        {
            Write-Verbose "Found 32bit Office 2013"
            $OfficeYear = "2013"
            $ProgramFilesLocation = "C:\Program Files (x86)"
            $Bitness = "x32"
        }
        elseif ((Test-path "\\$ComputerName\c$\Program Files\Microsoft Office\Office15\LIBRARY\") -eq $true)
        {
            Write-Verbose "Found 64bit Office 2013"
            $OfficeYear = "2013"
            $ProgramFilesLocation = "C:\Program Files"
            $Bitness = "x64"   
        }
     
    }
elseif ((Test-path "\\$ComputerName\c$\Program Files\Microsoft Office\root\Office16\LIBRARY\") -eq $true)
        {
            Write-Verbose "No existing 32bit Office files on $ComputerName."
            Write-Verbose "Found 64bit Office 2016"
            $ProgramFilesLocation = "C:\Program Files"
            $Bitness = "x64"
            $OfficeYear = "2016"   
        }

switch ($OfficeYear)
        {
         "2016"
             {
        $OfficeFolder = "$ProgramFilesLocation\Microsoft Office\root\Office16"
        $RegistryLocation = "\Software\Microsoft\Office\16.0"
        $RegistryNumber = "16.0"
             }
         "2013"
            {       
            $OfficeFolder = "$ProgramFilesLocation\Microsoft Office\Office15"
            $RegistryLocation = "\Software\Microsoft\Office\15.0"
            $RegistryNumber = "15.0"
            } 
         "2010"
            {
            $OfficeFolder = "$ProgramFilesLocation\Microsoft Office\Office14"
            $RegistryLocation = "\Software\Microsoft\Office\14.0"
            $RegistryNumber = "14.0"
            }
        }

     


if ($ValueTypeReturn -eq "Office Year")
    {
    $VerboseString = $ComputerName + " has Office " + $OfficeYear
    Write-Verbose $VerboseString
    Write-Output $OfficeYear
    }
elseif ($ValueTypeReturn -eq "Office Folder")
    {
    $VerboseString = $ComputerName + " has Office installed in " + $OfficeFolder
    Write-Verbose $VerboseString
    Write-Output $OfficeFolder
    }
elseif ($ValueTypeReturn -eq "Registry Location")
    {
    $VerboseString = $RegistryLocation + " is the registry location for Office for " + $ComputerName
    Write-Verbose $VerboseString
    Write-Output $RegistryLocation
    }
elseif ($ValueTypeReturn -eq "Registry Number")
    {
    $VerboseString = $RegistryNumber + " is the registry number for Office for " + $ComputerName
    Write-Verbose $VerboseString
    Write-Output $RegistryNumber
    }
elseif ($ValueTypeReturn -eq "Bitness")
    {
    $VerboseString = $Bitness + " is the bitness for Office for " + $ComputerName
    Write-Verbose $VerboseString
    Write-Output $Bitness
    }
}
else
{
Write-Error "$ComputerName is not detected on the network, please check the connection and try again"
}
}

Comments

Popular posts from this blog

SCCM PXE Boot Issues - No Advertisements Found

SCCM PXE Boot Issues - UEFI and Secure Boot

Application Doesn't Install During OSD Task Sequence in SCCM