Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Last active July 17, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chirishman/bd68d681f06a404a88f9ff102f298deb to your computer and use it in GitHub Desktop.
Save Chirishman/bd68d681f06a404a88f9ff102f298deb to your computer and use it in GitHub Desktop.
Get all metadata for old .doc files - Prototype
#* Setup
$BaseSelect = @(
'Directory',
'Extension',
'BaseName',
'Length',
'LastWriteTime',
'FullName'
)
[System.Collections.ArrayList]$ExpandedSelect = $BaseSelect.Clone()
[System.Collections.ArrayList]$Result = [System.Collections.ArrayList]::new()
$FileListLoc = 'd:\temp\DocFiles.xlsx'
$AllProperties = @(
'Title',
'Subject',
'Author',
'Keywords',
'Comments',
'Template',
'Last Author',
'Revision Number',
'Application Name',
'Last Print Date',
'Creation Date',
'Last Save Time',
'Total Editing Time',
'Number of Pages',
'Number of Words',
'Number of Characters',
'Security',
'Category',
'Format',
'Manager',
'Company',
'Number of Bytes',
'Number of Lines',
'Number of Paragraphs',
'Number of Slides',
'Number of Notes',
'Number of Hidden Slides',
'Number of Multimedia Clips'
)
$AllProperties | %{
[void]$ExpandedSelect.Add(@{
[string]'Name' = "$_";
[string]'Expression' = $([scriptblock]::Create("Try {`n`$pn = [System.__ComObject].invokemember('item',`$binding::GetProperty,`$null,`$BuiltinProperties,'$_')`n`$value = [System.__ComObject].invokemember('value',`$binding::GetProperty,`$null,`$pn,`$null)`n`$value`n} Catch [system.exception] {}"))
})
}
Push-Location $PSScriptRoot
#* Capture Initial File list
gci -File -Recurse |
select Directory,Extension,BaseName,Length,LastWriteTime,FullName |
Export-Excel $FileListLoc -TableStyle Light2 -ClearSheet -AutoSize
$table = import-excel $FileListLoc
# $table | ?{$_.Extension -eq '.doc'} | measure
#* Initialize Word
$application = New-Object -ComObject word.application
$application.Visible = $false
$binding = "System.Reflection.BindingFlags" -as [type]
#* Step through Documents
$table | select $BaseSelect | %{
$document = $application.documents.open($_.fullname)
start-sleep -s 20
$BuiltinProperties = $document.BuiltInDocumentProperties
[void]$Result.Add(($_ | select -Property $NewColumns))
$document.close()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($BuiltinProperties)
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($document)
Remove-Variable -Name document, BuiltinProperties
}
#* Export Results
$Result | Export-Excel $FileListLoc -TableStyle Light2 -ClearSheet -AutoSize
#* Cleanup
$application.quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($application) | Out-Null
Remove-Variable -Name application
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Pop-Location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment