Use PowerShell to Create Documents from Template


Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create Word documents from a template.

Hey, Scripting Guy! Question Hey, Scripting Guy! I often need to create a bunch of Word documents. These documents have a number of standard features. Right now, I basically paste the last document I was working on into the folder, rename the file, open the file, delete all of the custom stuff, and leave the boilerplate stuff.

I know there should be an easier way to do this, but to be honest I am doing the work of three people, and I simply do not have time to figure out an easier way to do this. Now the process takes me less than five minutes to get ready for my new report. So anything you come up with would have to take less time than that, or else it is simply not worth my time. You seem to be a clever geek, can you help me? Thanks,

—RC

Hey, Scripting Guy! AnswerHello RC,

Microsoft Scripting Guy, Ed Wilson, is here. I used to try to do a lot of different stuff with Microsoft Word. In fact, I rarely used Normal.dot because I did not like the way it formatted stuff. I never used standard headings, and I seemed to fight with every version of Word until I finally just gave up. No, it just does not seem to be worth the effort. Besides, whenever it seems like I get something going the way that I really want it, everything changes. Instead, I just try to go with the flow.

RC, it seems like your situation would be a good candidate for creating a Word template. The problem is that templates get rather complicated. Instead, I just use a normal Word document. I make changes to that document, and then I give it the name of a template. I do not save this with a template extension—I just call the basic Word document a template.

Here is a Word document that I use as a template. I have adjusted the default font, font size, margins, and the spacing between paragraphs. Each new document has the same information in the upper-left corner of the document.

Image of menu

Suppose that Professor Hasenpfeffer lists the Shakespearean plays that we will read this semester, and the good professor has assigned short reader response papers for each play. Here is the list of plays from the class syllabus:

Image of list

Because I have my class template paper, and because I have my list of paper topics, I can create the seven paper templates that I will use when I write my reader response papers. This does not even require a script. But, because the line of code would wrap several times, I decide to type the command into the Windows PowerShell ISE. Here is the command:

Get-Content C:\Lit\Shakespeare.txt |

ForEach-Object {

Copy-Item -Path C:\lit\PaperTemplate.docx -Destination  `

 ("c:\lit\{0}_ew.docx" -f $_)}

This is a single-line command, but I wrote it on several lines so it would be easier to read. Basically, I read the contents of the text file. For each line in the text file, I copy my PaperTemplate.docx file. I then rename the file to be the name of the play, and I add my initials as part of the name of the document. I use a place holder for the name of the play that comes across the pipeline. Here is the folder after I run my command:

Image of menu

Now all I need to do is read the play, type my reactions, and I am done. Well, I probably still need to figure out the MLA handbook bibliography entry for Shakespearean plays, but other than that, I am done.

Later I will use a CSV file for input and modify my template so that Windows PowerShell gives me a better head start on my papers.

To script or not to script...that is the question.

RC, that is all there is to using Windows PowerShell to create Word documents from a template. Word Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (0)

Skip to main content