{"id":7811,"date":"2015-02-16T00:01:00","date_gmt":"2015-02-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/02\/16\/use-powershell-to-back-up-word-docs\/"},"modified":"2019-02-18T10:30:36","modified_gmt":"2019-02-18T17:30:36","slug":"use-powershell-to-back-up-word-docs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-back-up-word-docs\/","title":{"rendered":"Use PowerShell to Back Up Word Docs"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to make a backup of all Word documents.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I need some help. I routinely edit Microsoft Word documents, but because of the changes I make to the file, I like to keep the original file intact. So what I do is open a Word document, then I use the <b>Save As<\/b> feature in Word to save a copy of the file into a different folder. I am wondering if I can have Windows PowerShell use the Word automation feature to do a <b>Save As<\/b> for me. Can you help me?<\/p>\n<p>&mdash;GB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello GB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I spent nearly a dozen years in consulting work before I became the Scripting Guy. One of the first things I learned to ask was the question &ldquo;Why?&rdquo; It is not that I doubted the customer, but rather because sometimes a customer becomes fixated on a specific solution. When I know what the purpose of the activity is, I can often recommend an easier (and most cases, less expensive) solution.<\/p>\n<p>GB, your question falls directly into this category, and it gives me a reason to begin Word Week.<\/p>\n<p>Sure, I could use the Word automation model. I can open a Word document in a folder, call <b>Save As<\/b>, give it a new name, then close the Word document and open to the next one. But in this case, the question &ldquo;Why?&rdquo; really comes into play. Why do you want to do this?<\/p>\n<p>Well, luckily you included the answer: You want to have a backup copy of a Word document before you begin editing. So, this actually makes it pretty easy. All I need is basic Windows PowerShell.<\/p>\n<p>I can use <b>Get-ChildItem<\/b> to recurse through your directory structure, <b>New-Item<\/b> to create your backup folder, and <b>Copy-Item<\/b> to copy the documents to the backup folder. This will be much faster, and easier to do, than using the Word automation model to open and Save As each document in the folder. Much, much easier. So let&#039;s get started.<\/p>\n<h2>Create the backup folder<\/h2>\n<p>The first thing I do is use the <b>New-Item<\/b> cmdlet to create a new folder that I will use as the backup folder. To do this, I specify the item type as <b>Directory<\/b> and provide a path. At the same time, I use a couple of variables to specify the destination path for the source files and the backup folder location. I use <b>&ndash;WhatIf<\/b> now so the command does not actually execute. This will help me make sure the script works completely before I actually run it. These commands are shown here:<\/p>\n<p style=\"margin-left:30px\">$bu = &quot;e:\\backup&quot;<\/p>\n<p style=\"margin-left:30px\">$source = &quot;E:\\Data\\ScriptingGuys\\2015&quot;<\/p>\n<p style=\"margin-left:30px\">New-Item -ItemType directory -Path $bu -WhatIf<\/p>\n<h2>Find the files<\/h2>\n<p>Now I need to find the document files. I have specified my directory starting point as a string that I stored in the <b>$source<\/b> variable. I filter out only files that have an extension of .docx, and I tell the cmdlet to recurse so that it will find all files in subfolders off of the main directory. I end the command with a pipe character ( <b>|<\/b> ) because I want each of the file objects that I find to pass to the next command. Here is the command:<\/p>\n<p style=\"margin-left:30px\">Get-ChildItem -Path $source -Filter *.docx -Recurse |<\/p>\n<h2>Copy the files and give them a new name<\/h2>\n<p>I want to copy each file that I find to the backup destination, and I want to add the letters &ldquo;bu&rdquo; to the end of the file name. To do this, I use the <b>Foreach-Object<\/b> cmdlet and the <b>Copy-Item<\/b> cmdlet. Once again, because I want to see that the command works properly first, I add the <b>&ndash;WhatIf<\/b> parameter to <b>Copy-Item<\/b>,<b> <\/b>&nbsp;as shown here:<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">Copy-Item -path $_.FullName -Destination (&quot;$bu\\{0}.bu.docx&quot; -f $_.basename) -whatif }<\/p>\n<p>I create the new file name based on the <b>BaseName<\/b> property of the file. The <b>BaseName<\/b> property is simply the file name without the file extension and without the file path. I pick up the backup location from the <b>$bu<\/b> variable, and I add .bu to the base file name. I then retain the .docx file extension. The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">$bu = &quot;e:\\backup&quot;<\/p>\n<p style=\"margin-left:30px\">$source = &quot;E:\\Data\\ScriptingGuys\\2015&quot;<\/p>\n<p style=\"margin-left:30px\">New-Item -ItemType directory -Path $bu -WhatIf<\/p>\n<p style=\"margin-left:30px\">Get-ChildItem -Path $source -Filter *.docx -Recurse |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">Copy-Item -path $_.FullName -Destination (&quot;$bu\\{0}.bu.docx&quot; -f $_.basename) -whatif }<\/p>\n<p>I run it, and examine the output in the Windows PowerShell ISE. This output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-16-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-16-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I pay attention to the second line of the output, and then to the third and following lines. They tell me that the script will work as I expect, so I remove the two <b>&ndash;WhatIf<\/b> parameters. The revised script is shown here:<\/p>\n<p style=\"margin-left:30px\">$bu = &quot;e:\\backup&quot;<\/p>\n<p style=\"margin-left:30px\">$source = &quot;E:\\Data\\ScriptingGuys\\2015&quot;<\/p>\n<p style=\"margin-left:30px\">New-Item -ItemType directory -Path $bu<\/p>\n<p style=\"margin-left:30px\">Get-ChildItem -Path $source -Filter *.docx -Recurse |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">Copy-Item -path $_.FullName -Destination (&quot;$bu\\{0}.bu.docx&quot; -f $_.basename) }<\/p>\n<p>This time, the output is less impressive:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-16-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-16-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now I check to see my newly created backup folder. As you can see in the following image, it is full of backup documents:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-16-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-16-15-03.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>GB, that is all there is to using Windows PowerShell to back up your Word documents. Word Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to make a backup of all Word documents. &nbsp;Hey, Scripting Guy! I need some help. I routinely edit Microsoft Word documents, but because of the changes I make to the file, I like to keep the original file intact. So what I do is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[343,567,3,45],"class_list":["post-7811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-backup","tag-files-and-folders","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to make a backup of all Word documents. &nbsp;Hey, Scripting Guy! I need some help. I routinely edit Microsoft Word documents, but because of the changes I make to the file, I like to keep the original file intact. So what I do is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7811","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7811"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7811\/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=7811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}