Search code examples
powershellactive-directoryms-office

Get a List of MS Office


Ok, I am trying to get a list off all Office versions and how many of each. We are migrating to Windows 10 and I am trying to talk him into upgrading office to 2016. We have Office as old as 2010. I need a list of how many of each version we have. Even if i can get a list of what computer has what version. I am trying not to run an audit on every computer individually, we have 200 computers.

I have tried several different approaches.

Get-ADComputer -Filter * -Property * | Select-Object Name |
 Export-CSV ADcomputerslist.csv -NoTypeInformation -Encoding UTF8

This doesnt actually save to a file

foreach ($computer in (Get-Content "c:\computers.txt")){
  Write-Verbose "Working on $computer..." -Verbose
  Invoke-Command -ComputerName "$Computer" -ScriptBlock {
    Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365ProPlusRetail* |
    Select-Object DisplayName, DisplayVersion, Publisher
  } | export-csv C:\results.csv -Append -NoTypeInformation
}

Solution

  • It's generally considered unsafe to use Get-WmiObject to check the Win32_Product class because this can unintentionally trigger repair installs on software. It's safer to check the registry for installed programs:

    # We need to check for both 64-bit and 32-bit software
    $regPaths = "HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall",
      "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    
    # Get the name of all installed software registered in the registry with Office in the name
    # (you can search for exact strings if you know them for specific versions)
    $regPaths | Foreach-Object {
      ( Get-ItemProperty "${_}\*" DisplayName -EA SilentlyContinue ).DisplayName | Where-Object {
        $_ -match 'office'
      }
    }
    

    The way this works is that for both registry paths, we want to get the DisplayName value every key underneath the base path from $regPaths (these are mostly going to be GUID-named keys, not much value for just identifying the software by name). We ignore errors since they will clutter the output and for this operation it's expected that some keys may not have a DisplayName property. We don't care about those.

    Once we have the DisplayName enumerated for all of the subkeys, we want to filter out the ones that do not have 'Office' in the name. Note that the -match operator is case-insensitive, so casing doesn't matter here. So the Where-Object clause only returns a DisplayName of it finds the string office in it. You can tweak this regular expression if you know exact DisplayName strings for each version of Office you support, as inherently this will return anything with Office in the name.