Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Created July 21, 2021 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1RedOne/40e219417259ab40af55f7bcf5ba0689 to your computer and use it in GitHub Desktop.
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.

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