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':
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()
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()