{"id":52273,"date":"2009-10-12T00:01:00","date_gmt":"2009-10-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/10\/12\/hey-scripting-guy-how-do-i-count-the-number-of-words-in-a-group-of-office-word-documents\/"},"modified":"2009-10-12T00:01:00","modified_gmt":"2009-10-12T00:01:00","slug":"hey-scripting-guy-how-do-i-count-the-number-of-words-in-a-group-of-office-word-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-do-i-count-the-number-of-words-in-a-group-of-office-word-documents\/","title":{"rendered":"Hey, Scripting Guy! How Do I Count the Number of Words in a Group of Office Word Documents?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" width=\"125\" height=\"16\" style=\"border: 0px\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\"><\/a>  <\/p>\n<p><img decoding=\"async\" width=\"34\" height=\"34\" title=\"Hey, Scripting Guy! Question\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" border=\"0\"><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">Hey, Scripting Guy! I have a folder that contains a number of Microsoft Word documents that are all related to a specific project. I need to know the number of words in all of those Word documents. This is because the project is billable based upon the number of words. In the past, I used Explorer to open each file to get the word count of each Microsoft Word document. I then added them up with calculator. This was a time-consuming and tedious process. But when you are dealing with money, you do not mind taking a little extra effort. The problem is this new project is a very large project with hundreds of document files. To make matters worse, the project folder also has Microsoft Excel files, Microsoft Project files, bitmap images, and assorted Notepad text files. It is a mess, and I do not want to waste several hours on this if I can help it. Can you help me please?<\/p>\n<p>&lt;<\/p>\n<p>p class=&#8221;MsoNormal&#8221; style=&#8221;margin: 0in 0in 8pt;&#8221;&gt;&#8211; WS<\/p>\n<p><img decoding=\"async\" width=\"34\" height=\"34\" title=\"Hey, Scripting Guy! Answer\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" border=\"0\"><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">Hello WS,<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">Microsoft Scripting Guy Ed Wilson here. You know, contrary to popular belief, Scripting Guys are not paid by the word. A long time ago when I wrote for a newspaper, I was paid by the word. I have also been paid by the word for magazine articles, so your question is not something that is completely off the wall. In addition to being paid by the word, when I write a proposal for a new book project, I must supply an estimated word count for the new book. This is used to determine estimated page count, which the book publisher can use as a guide for setting the cost of new book. When writing the book, if my chapters are tending to be either too long or too short, my developmental editor will start to get excited.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">Currently, I am not too excited. I am sipping a cup of English breakfast tea with a cinnamon stick in it and a bit of lemon grass. I am listening to &ldquo;Travelin&#8217; Blues&rdquo; by <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dave_Brubeck\"><span style=\"font-family: Segoe\">Dave Brubeck<\/span><\/a> on my Zune.<\/p>\n<p class=\"Readeraidonly\" style=\"margin: 15pt 12pt 0.25in 0in\">Truth in Blogging notice to comply with recent <a href=\"http:\/\/www.ftc.gov\/opa\/2009\/10\/endortest.shtm\">USA FTC requirements<\/a>: I purchased my Zune using my own money. It was not a gift, a bribe, or a prize for blogging about the device. I also purchased the Scripting Wife&#8217;s Zune as well. I bought both of these devices because they are cool!<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">The grass outside is damp from the evening&rsquo;s rain, and patches of fog lounge on the lawn like deer near a stream in the deep woods. While listening to &ldquo;Linus and Lucy,&rdquo; also by Dave Brubeck, I wrote the CountWordsInWord.ps1 script seen here.<\/p>\n<p class=\"CodeBlockScreenedHead\" style=\"margin: 15pt 12pt 0pt 0in\"><strong>CountWordsInWord.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin: 15pt 17.3pt 0.25in 0in\"><span style=\"font-size: 9.5pt\"><span style=\"background-color: #f2f2f2\"><span style=\"font-family: Lucida Sans Typewriter\">Function Set-Variables<br>{<br><span>&nbsp;<\/span>$folderpath = &#8220;c:fso*&#8221;<br><span>&nbsp;<\/span>$fileTypes = &#8220;*.docx&#8221;,&#8221;*doc&#8221;<br><span>&nbsp;<\/span>$confirmConversion = $false<br><span>&nbsp;<\/span>$readOnly = $true<br><span>&nbsp;<\/span>$addToRecent = $false<br><span>&nbsp;<\/span>$passwordDocument = &#8220;password&#8221;<br><span>&nbsp;<\/span>$wordCountFile = &#8220;C:fsowordCount.csv&#8221;<br><span>&nbsp;<\/span>$numberOfWords = 0<br><span>&nbsp;<\/span>Set-OutPutFile<br>} #end Set-Variables<\/p>\n<p>Function Set-OutputFile<br>{<br><span>&nbsp;<\/span>if(Test-Path -path $wordCountFile)<br><span>&nbsp;&nbsp; <\/span>{ Remove-Item -path $wordCountFile }<br><span>&nbsp;<\/span>&#8220;name,wordCount&#8221; &gt;&gt; $wordCountFile<br><span>&nbsp;<\/span>Get-WordDocuments<br>} #end Set-OutputFile<\/p>\n<p>Function Get-WordDocuments<br>{<br><span>&nbsp; <\/span>&#8220;Counting Words in Word Docs in $folderPath&#8221;<br><span>&nbsp;<\/span>$word = New-Object -ComObject word.application<br><span>&nbsp;<\/span>$word.visible = $false<br><span>&nbsp;<\/span>Get-ChildItem -path $folderpath -include $fileTypes |<br><span>&nbsp;<\/span>foreach-object `<br><span>&nbsp; <\/span>{<br><span>&nbsp;&nbsp; <\/span>$path =<span>&nbsp; <\/span>($_.fullname).substring(0,($_.FullName).lastindexOf(&#8220;.&#8221;))<br><span>&nbsp;&nbsp; <\/span>$doc = $word.documents.open($_.fullname, $confirmConversion, $readOnly, <br><span>&nbsp;&nbsp; <\/span>$addToRecent,<span>&nbsp;&nbsp; <\/span>$passwordDocument)<br><span>&nbsp;&nbsp; <\/span>&#8220;<span>&nbsp; <\/span>$($_.name), $($doc.words.count)&#8221;<span>&nbsp; <\/span>&gt;&gt; $wordCountFile<br><span>&nbsp;&nbsp; <\/span>$doc.close()<br><span>&nbsp; <\/span>} #end Foreach-Object<br><span>&nbsp;<\/span>$word.Quit()<br><span>&nbsp;<\/span>Get-WordCount<br>} #end Get-WordDocuments<\/p>\n<p>Function Get-WordCount<br>{<br><span>&nbsp;<\/span>$wdcsv = import-csv -path $wordCountFile<br><span>&nbsp;<\/span>for ($i = 0 ; $i -le $wdcsv.length -1 ; $i++)<br><span>&nbsp;<\/span>{<br><span>&nbsp; <\/span>$numberOfWords += [int32]$wdcsv[$i].wordCount<br><span>&nbsp;<\/span>}<br><span>&nbsp;<\/span>$numberOfWords<br>} #end Get-WordCount<\/p>\n<p># *** Entry Point to Script ***<\/p>\n<p>Set-Variables<\/p>\n<p><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">The CountWordsInWord.ps1 script contains a series of functions, each of which calls another function until the word count report is produced. This is not done to promote code reuse (which is one reason for writing functions), but to make the script easier to read and to understand (another reason for writing functions). You will notice the script is a bit complicated. If a similar script were written using VBScript, it would not be significantly different. The main advantage of using Windows PowerShell for this script is that the <b>Get-ChildItem<\/b> cmdlet is easier to use than the <b>FileSystemObject<\/b>. We have a few other advantages due to the more compact syntax of Windows PowerShell over VBScript. The complexity of the script is due to using the Microsoft Word automation objects to access the word count of the Word documents.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">The first function in the CountWordsInWord.ps1 script is called <b>Set-Variables<\/b>. As the name implies, it sets the values of variables that will be used in the script. A variable table is a good way to keep track of a large number of variables in a script. The variable table for the CountWordsInWord.ps1 script is seen in Table 1.<\/p>\n<p class=\"TableNum-Title\" style=\"margin: 3pt 0in\"><strong>Table 1<span>&nbsp; <\/span>Variable Table<\/strong><\/p>\n<table class=\"MsoNormalTable\" style=\"margin: auto auto auto 5.4pt;border: currentColor;border-collapse: collapse\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;padding-top: 0in;border: windowtext 1pt solid\">\n<p class=\"TableHead\" style=\"margin: 0in 0in 2pt\"><strong>Variable<\/p>\n<p><\/strong><\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: windowtext 1pt solid;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableHead\" style=\"margin: 0in 0in 2pt\"><b>Initial value<\/p>\n<p><\/b><\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: windowtext 1pt solid;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableHead\" style=\"margin: 0in 0in 2pt\"><b>Use in script<\/p>\n<p><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$folderpath<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>&#8220;c:fso*&#8221;<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Path to search for Word documents.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$fileTypes<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>&#8220;*.docx&#8221;,&#8221;*doc&#8221;<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Two types of Word documents to look for.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$confirmConversion<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>$false<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Used by Open method of Word. Do not prompt if conversion is needed.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$readOnly<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>$true<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Used by Open method of Word. Open document read-only.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$addToRecent<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>$false<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Used by Open method of Word. Do not add to recently used.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$passwordDocument<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>&#8220;password&#8221;<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Used by Open method of Word. The password to use for any password protected documents.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$wordCountFile<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>&#8220;C:fsowordCount.csv&#8221;<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">The path for the output file that holds file names and word count.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"156\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: windowtext 1pt solid;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 117pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">$numberOfWords<\/p>\n<\/td>\n<td width=\"150\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 112.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\"><span>&nbsp;<\/span>0<\/p>\n<\/td>\n<td width=\"318\" valign=\"top\" style=\"border-bottom: windowtext 1pt solid;border-left: #f0f0f0;padding-bottom: 0in;background-color: transparent;padding-left: 5.4pt;width: 238.5pt;padding-right: 5.4pt;border-top: #f0f0f0;border-right: windowtext 1pt solid;padding-top: 0in\">\n<p class=\"TableText\" style=\"margin: 0in 0in 2pt\">Used to hold the total number of words for all Word documents.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">After all of the variables have been initialized, the <b>Set-Variable<\/b> function calls the <b>Set-OutPutFile<\/b> function. By calling the <b>Set-OutPutFile<\/b> function from within the <b>Set-Variables<\/b> function, all of the variables will be made available to the <b>Set-OutPutFile<\/b> function. This is because the <b>Set-Variables<\/b> function will become the parent namespace for the <b>Set-OutPutFile<\/b> function. All variables that are created in the <b>Set-Variables<\/b> namespace will be available within the <b>Set-Variables<\/b> namespace, as well as child namespaces. If, of course, the variables were marked as private, they would only be available within the <b>Set-Variables<\/b> function. WS, you will need to modify the value of the <b>$folderpath<\/b> variable and the <b>$wordCountFile<\/b> variable to match your computer. The <b>Set-Variables<\/b> function is seen here:<\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;background: #f2f2f2\"><span style=\"font-size: 9.5pt\"><span style=\"font-family: Lucida Sans Typewriter\">Function Set-Variables<br>{<br><span>&nbsp;<\/span>$folderpath = &#8220;c:fso*&#8221;<br><span>&nbsp;<\/span>$fileTypes = &#8220;*.docx&#8221;,&#8221;*doc&#8221;<br><span>&nbsp;<\/span>$confirmConversion = $false<br><span>&nbsp;<\/span>$readOnly = $true<br><span>&nbsp;<\/span>$addToRecent = $false<br><span>&nbsp;<\/span>$passwordDocument = &#8220;password&#8221;<br><span>&nbsp;<\/span>$wordCountFile = &#8220;C:fsowordCount.csv&#8221;<br><span>&nbsp;<\/span>$numberOfWords = 0<br><span>&nbsp;<\/span>Set-OutputFile<br>} #end Set-Variables<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">In the <b>Set-OutputFile<\/b> function, the path to the <b>$wordCountFile<\/b> file is checked. If the file exists, it is deleted. A new file is then created by using the redirection arrows. The header for the wordCount.csv file is <b>name<\/b> and <b>wordCount<\/b>. The header for the file is written at the same time that the file is created. After creating the wordCount.csv file, the <b>Set-OutputFile<\/b> function calls the <b>Get-WordDocuments<\/b> function. The <b>Set-OutputFile<\/b> function is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;background: #f2f2f2\"><span style=\"font-size: 9.5pt\"><span style=\"font-family: Lucida Sans Typewriter\">Function Set-OutputFile<br>{<br><span>&nbsp;<\/span>if(Test-Path -path $wordCountFile)<br><span>&nbsp;&nbsp; <\/span>{ Remove-Item -path $wordCountFile }<br><span>&nbsp;<\/span>&#8220;name,wordCount&#8221; &gt;&gt; $wordCountFile<br><span>&nbsp;<\/span>Get-WordDocuments<br>} #end Set-OutputFile<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">The <b>Get-WordDocuments<\/b> function is the function that interacts with the Word automation object model. The first thing the <b>Get-WordDocuments<\/b> function does after displaying a status message on the Windows PowerShell console is create an instance of the <b>word.application<\/b> object. This object is the main object you use when working with Microsoft Word. The <b>word.application<\/b> object has a number of methods and properties available to it, which are all documented on <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb148369.aspx\"><span style=\"font-family: Segoe\">MSDN<\/span><\/a>. The <b>visible<\/b> property of the application object is set to <b>$false<\/b> because there is no need to pop up a hundred or more Microsoft Word documents when all you want to do is to obtain the word count from the document. This part of the <b>Get-WordDocuments<\/b> function is seen here:<\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;background: #f2f2f2\"><span style=\"font-size: 9.5pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;<\/span>&#8220;Counting Words in Word Docs in $folderPath&#8221;<br><span>&nbsp;<\/span>$word = New-Object -ComObject word.application<br><span>&nbsp;<\/span>$word.visible = $false<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">You can use the <b>Get-ChildItem<\/b> cmdlet from Windows PowerShell to obtain a listing of all of the Microsoft Word documents in the specified folder. Each of the found files is piped to the <b>ForEach-Object<\/b> cmdlet where the path to file is obtained. This is seen here:<\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;background: #f2f2f2\"><span style=\"font-size: 9.5pt\"><span style=\"font-family: Lucida Sans Typewriter\">Get-ChildItem -path $folderpath -include $fileTypes |<br><span>&nbsp;<\/span>foreach-object `<br><span>&nbsp; <\/span>{<br><span>&nbsp;&nbsp; <\/span>$path =<span>&nbsp; <\/span>($_.fullname).substring(0,($_.FullName).lastindexOf(&#8220;.&#8221;))<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt\">After the path to the Microsoft Word document has been retrieved, you can use it with the <b>open<\/b> method from the <b>documents<\/b> collection. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211902.aspx\"><span style=\"font-family: Segoe\">documents collection object<\/span><\/a> contains the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb216319.aspx\"><span style=\"font-family: Segoe\">open method<\/span><\/a> and is retrieved by querying the <b>documents<\/b> property from the <b>word.application<\/b> object. After the document is open, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb212252.aspx\"><span style=\"font-fami\"><\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a folder that contains a number of Microsoft Word documents that are all related to a specific project. I need to know the number of words in all of those Word documents. This is because the project is billable based upon the number of words. In the past, I used [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,45,395],"class_list":["post-52273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-windows-powershell","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a folder that contains a number of Microsoft Word documents that are all related to a specific project. I need to know the number of words in all of those Word documents. This is because the project is billable based upon the number of words. In the past, I used [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=52273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52273\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=52273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}