4

I try to get the properties (owner) of a file with the line of code in PowerShell:

    $file = "\\networkshare\directory\file.doc"
    Get-ItemProperty -Path $file | Format-list -Property * -Force

It is easy to extract the owner, modified date, etc. But I want to extract the "last saved by' and 'revision number':

enter image description here

Update:

The following code seems to work. But every time I run the script it changes the value of "last saved by". How to prevent this and only read the property?

 $word = New-Object -Com Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Documents.Open("\\networkshare\directory\file.doc")

$binding = "System.Reflection.BindingFlags" -as [type]
Foreach($property in $doc.BuiltInDocumentProperties) {
   try {
      $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
      if ($pn -eq "Last author") {
         $lastSaved = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
         echo "Last saved by: "$lastSaved      
      }        }
   catch { }
}

$doc.Close()
$word.Quit()
R3uK
  • 14,417
  • 7
  • 43
  • 77
Citizen SP
  • 1,411
  • 7
  • 36
  • 68
  • 1
    I don't have the time to do up a proper answer right now (stupid meetings) but if you google ***powershell extended file properties*** you will find some examples of using the Shell.Application COM object to do this. – EBGreen Dec 17 '15 at 14:49
  • @EBGreen see updated question above. I added an additional question – Citizen SP Dec 17 '15 at 14:52
  • Did you try the Shell.Application COM object as I suggested? – EBGreen Dec 17 '15 at 14:53

1 Answers1

4

This happens because you are saving the document when you call $doc.Close()

Simply call Close with SaveChanges to false:

$doc.Close($false)

Your code (I also added the open in read only mode):

$word = New-Object -Com Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Documents.Open("\\networkshare\directory\file.doc", $false, $true) # open in read only mode

$binding = "System.Reflection.BindingFlags" -as [type]
Foreach($property in $doc.BuiltInDocumentProperties) {
   try {
      $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
      if ($pn -eq "Last author") {
         $lastSaved = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
         echo "Last saved by: "$lastSaved      
      }        }
   catch { }
}

$doc.Close($false) 
$word.Quit()
Fabian
  • 1,886
  • 14
  • 13