(Update–I forgot to thank Joel Bennett, he was a great help in debugging some of the syntax below.)
I recently thought to grab the page count of some Word docs I’ve been working on lately. I know the data is already available outside of Word. You’ve long been able to view a pages column in Explorer which displays this information. The question was–how to get at it using PowerShell? I looked around for a little but I was apparently not using the right keywords because I found nutin’. I didn’t have any more time to dedicate to the search, so I asked the experts on the PowerShell newsgroup, of which I consider myself a sometimes-member
.
Shay quickly replied with this link to an old article from Keith Hill titled, “MSH: Get Extended Properties of a File“. In fact, the article was so old it referred to PowerShell by its former codename, MSH which was short for Monad Shell. Some of these older MSH examples need sprucing up to work with version 1.0. This one I think would’ve worked ok, but it definitely did not take advantage of one or two cmdlets that did not exist at the time the article was written. Regardless, I had a slightly different end goal so I just took the core technique, which is to use the GetDetailsOf method of the Shell.Application COM object, and applied it to something new–PS1XML type formatting.
The short version: you can make a specially-formatted XML file which contains instructions to the type formatter which determines how methods and properties are displayed. In addition to manipulating how the existing ones are displayed, you can add new methods and properties (technically, they’re different member types: script methods and script properties). These members can contain script blocks. I’ll show you the code and a screenshot–that should illustrate the point pretty well.
1: <?xml version="1.0" encoding="utf-8" ?>
2: <Types>
3: <Type>
4: <Name>System.IO.FileInfo</Name>
5: <Members>
6: <ScriptProperty>
7: <Name>Pages</Name>
8: <GetScriptBlock>
9: $shellApp = new-object -com shell.application
10: $myFolder = $shellApp.Namespace($this.Directory.FullName)
11: $fileobj = $myFolder.Items().Item($this.Name)
12: "$($myFolder.GetDetailsOf($fileobj,13))"
13: </GetScriptBlock>
14: </ScriptProperty>
15: </Members>
16: </Type>
17: </Types>
You can see the object type name on line 4: FileInfo objects, which is to say, files. Line 7 shows the name of our new script property: pages. Lines 9-12 are where the work is done, and as far as these things go, its rather simple. In line 12 you see the number “13″ there at the end, that’s the number which indicates the page number column. I got that number from Keith Hill’s article.
Lastly, to put this in action, save the above as a file with the extension PS1XML, and place it somewhere stable, like your $pshome folder. Then run “update-typedata <filename.ps1xml>”. That’s it.
Here’s what it looks like:
38# ls *.doc | ft name,pages
Name Pages
---- -----
ch 4 extract - wikideploy.doc 3
Managing VMware Infrastructure with Powe... 1
Pretty cool, huh?

No comments yet.