Tuesday, December 6, 2016

Reading Word document properties in PowerShell

Word documents include some extra properties like "total time spent editing." These properties aren't visible in Explorer because they're just data in the file. You could use COM to interop with Word and read them, but it's not super hard to grab the data directly from the file.

Modern Word documents are just ZIP files, mostly containing XML files, both of which PowerShell can handle. This little script outputs the total editing time of a given Word document:

$zip = [System.IO.Compression.ZipFile]::Open($filename, 'Read')
$propsentry = $zip.GetEntry('docProps/app.xml')
If ($propsentry -ne $null) {
    $stream = $propsentry.Open()
    $reader = New-Object System.IO.StreamReader $stream
    $content = $reader.ReadToEnd()
    $xmldoc = [xml]$content
    $xmldoc.Properties.TotalTime
}
$zip.Dispose()

The time is measured in minutes.

Based on my Super User answer.

No comments:

Post a Comment