# Get-Docstatistic.ps1 # v1.0 8/14/2008 # jhicks@sapien.com # http://blog.sapien.com # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. * * # **************************************************************** Function Get-DocStatistic { BEGIN { $word=New-Object -com "Word.Application" } PROCESS { $file=$_ $doc=$word.documents.open($file.Fullname) Write-Progress -Activity "Gathering document statistics" ` -Status "Analyzing $file" ` -currentoperation "Counting Words" $words=$doc.ComputeStatistics("wdStatisticWords") Write-Progress -Activity "Gathering document statistics" ` -Status "Analyzing $file" ` -currentoperation "Counting lines" $lines=$doc.ComputeStatistics("wdStatisticLines") Write-Progress -Activity "Gathering document statistics" ` -Status "Analyzing $file" ` -currentoperation "Counting paragraphs" $para=$doc.ComputeStatistics("wdStatisticParagraphs") Write-Progress -Activity "Gathering document statistics" ` -Status "Analyzing $file" ` -currentoperation "Counting pages" $pages=$doc.ComputeStatistics("wdStatisticPages") #close document discarding any changes $doc.close([ref]$false) $obj=New-Object PSObject $obj | Add-Member -MemberType NoteProperty -Name "Document" -Value $file $obj | Add-Member -MemberType NoteProperty -Name "Directory" -Value $file.directoryname $obj | Add-Member -MemberType NoteProperty -Name "Filename" -Value $file.Name $obj | Add-Member -MemberType NoteProperty -Name "Size" -Value $file.length $obj | Add-Member -MemberType NoteProperty -Name "Words" -Value $words $obj | Add-Member -MemberType NoteProperty -Name "Lines" -Value $lines $obj | Add-Member -MemberType NoteProperty -Name "Paragraphs" -Value $para $obj | Add-Member -MemberType NoteProperty -Name "Pages" -Value $pages write $obj } END { $word.Quit() } } #examples: # dir c:\test\*.docx | Get-DocStatistic # $stats=dir "$env:userprofile\documents\*" -include *.doc,*.docx -recurse| Get-DocStatistic # $stats | Measure-Object -Sum pages