Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Created July 21, 2021 15:48
Show Gist options
  • Select an option

  • Save 1RedOne/40e219417259ab40af55f7bcf5ba0689 to your computer and use it in GitHub Desktop.

Select an option

Save 1RedOne/40e219417259ab40af55f7bcf5ba0689 to your computer and use it in GitHub Desktop.
Get Office File Info In PowerShell

Easily access file propeties from Microsoft Office files

This PowerShell function lets you easily access Microsoft Office File Properties like DateLastSaved or Author or Rivision count!

Usage

  1. Load the function
  2. Call the function
  3. Love the function

Excel Support

C:\temp\Preferences> Get-OfficeFileInfo  C:\temp\UsersOfabc.comDomain.xlsx

Name             Exp                  
----             ---                  
Title                                 
Subject                               
Author                                
Keywords                              
Comments                              
Template                              
Last author      Stephen Owen         
Revision number                       
Application name Microsoft Excel      
Creation date    7/21/2021 11:30:51 AM
Last save time   7/21/2021 11:30:51 AM
Security         0                    
Category                              
Format                                
Manager                               
Company                               
Hyperlink base                        
Content type                          
Content status                        
Language                              
Document version    

Word Support

Get-OfficeFileInfo C:\temp\SomeDoct.docx

#Also works!
#https://stackoverflow.com/questions/34336486/get-file-last-saved-by-property-without-changing-it
function Get-OfficeFileInfo($filePath){
if ($filePath -like "*.doc*"){
Get-WordFileInfo $filePath
}
else{
Get-ExcelFileInfo $filePath
}
}
function Get-WordFileInfo($filePath){
$word = New-Object -Com Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Documents.Open($filePath, $false, $true) # open in read only mode
$binding = "System.Reflection.BindingFlags" -as [type]
Foreach($property in $doc.BuiltInDocumentProperties) {
try {
$name = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
$value = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
[psCustomObject]@{Name=$name ;Exp=$value}
}
catch { }
}
$doc.Close($false)
$word.Quit()
}
function Get-ExcelFileInfo($filePath){
$word = New-Object -Com Excel.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Workbooks.Open($filePath, $false, $true) # open in read only mode
$binding = "System.Reflection.BindingFlags" -as [type]
Foreach($property in $doc.BuiltInDocumentProperties) {
try {
$name = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
$value = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
[psCustomObject]@{Name=$name ;Exp=$value}
# $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
# if ($pn -eq "Last author") {
#
# echo "Last saved by: "$lastSaved
}
catch { }
}
$doc.Close($false)
$word.Quit()
}
@avishek235
Copy link

But is this work with the file server ? is it necessary to have office application installed on the file server & how to run if there is a multiple office files is there.

@amponsahh
Copy link

amponsahh commented Nov 9, 2025

  • Added PowerPoint support
  • Widened Word & Excel file support
  • Added/Improved error handling so if there's a problem, it doesn't just stop iterating through BuiltInDocumentProperties
  • Consolidated code to one function
  • Switched to arraylist to account for dynamic size
Function Get-OfficeFileInfo{
    param(
        [Parameter(Mandatory)]
        [System.Object]$filePath
    )

    #determine office app from ext
    $OfficeApp = $(If ($filePath -like "*.doc*" -or $filePath -like "*.odt" -or $filePath -like "*.rtf" -or $filePath -like "*.pdf" -or $filePath -like "*.wps") {"Word.Application"} 
                    Else {$(If ($filePath -like "*.xl*" -or $filePath -like "*.ods" ) {"Excel.Application"} 
                    Else {$(If ($filePath -like "*.pp*" -or $filePath -like "*.pot*" -or $filePath -like "*.odp" ) {"PowerPoint.Application"} Else {$null})})})
    
    try{
        $comVar = New-Object -ComObject $OfficeApp
    }catch{
        return "Expecting Word/Excel/PowerPoint filetypes`nCannot create new COM"
    }

    try{
        $comVar.Visible = 0
    }catch{
        Write-Host $_
        Write-Host "Can't hide application window`nNo worries, proceeding anyway"
    }
    
    If($OfficeApp -like "Word*"){
        $doc = $comVar.Documents.Open($filePath, $false, $true)
    }elseif($OfficeApp -like "Excel*"){
        $doc = $comVar.Workbooks.Open($filePath, $false, $true)
    }elseif($OfficeApp -like "PowerPoint*"){
        $doc = $comVar.Presentations.Open($filePath, $false, $true)
    }else{
        return "Filetype not supported!!`nOnly Word, PowerPoint, and Excel as of now"
    }
    
    $binding = "System.Reflection.BindingFlags" -as [type];
    $arrProps = [System.Collections.ArrayList]::new()
    Foreach($property in $doc.BuiltInDocumentProperties) {
        $errorThingX = 0
        try{
            $ThingA = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null) 
        }catch{
            $errorThingX = 1
        }
        try{
            $ThingB = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
        }catch{
            $errorThingX = 1
        }
        if ($errorThingX -ne 1){
            [void]$arrProps.Add([PSCustomObject]@{Name=$ThingA;Value=$ThingB})
        }
    }

    if($OfficeApp -like "PowerPoint*"){
        $doc.Close() 
    }else{
        $doc.Close($false) 
    }
    $comVar.Quit()

    return $arrProps
}
Export-ModuleMember -Function Get-OfficeFileInfo

This was implemented as a powershell module but you can just use the source without the last line.
If you'd like to keep it as a module, just store the source in a *.psm1 file and import it into your workspace or call it as necessary:
Import-Module "\path\to\file.psm1" -Verbose ]---> Verbose flag confirms the Get-OfficeFileInfo function was successfully pulled

Usage:
To get all properties of a file use:

Get-OfficeFileInfo -filePath "\path\to\Word\Excel\PowerPoint\document" 

To get a specific property:
call the function, pipe the properties, and get the value of said desired property like so:

(Get-OfficeFileInfo -filePath "\path\to\Word\Excel\PowerPoint\document" | ? Name -eq "Last save time").Value

TODO/Improvement:

  • Remove $OfficeApp variable and implement a way to attempt to open any applicable file via brute force that way you don't need to define filetypes manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment