{"id":11061,"date":"2012-02-23T00:01:00","date_gmt":"2012-02-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/02\/23\/use-powershell-to-back-up-modified-files-to-the-network\/"},"modified":"2012-02-23T00:01:00","modified_gmt":"2012-02-23T00:01:00","slug":"use-powershell-to-back-up-modified-files-to-the-network","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-back-up-modified-files-to-the-network\/","title":{"rendered":"Use PowerShell to Back Up Modified Files to the Network"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to back up modified files to the network.<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hey, Scripting Guy! I have a folder that contains files in it, some of which I modify on a daily basis. I am wondering if I can use Windows PowerShell to back up only the modified files&mdash;those that have changed that particular day?<\/p>\n<p>&mdash;NG<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hello NG,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I don&rsquo;t know if you have noticed it, but for the last couple of weeks, many of the topics have related to items that are to be covered in the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/04\/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx\" target=\"_blank\">2012 Scripting Games<\/a>. Registration is not yet open, and the PoshCode site for the 2012 Scripting Games is not yet up, but things are progressing along nicely. If you are planning to compete in the games, you should be reviewing the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/05\/2012-scripting-games-study-guide-a-resource-for-learning-powershell.aspx\" target=\"_blank\">2012 Scripting Games Study Guide<\/a>. (If the truth be told, even if you are not competing in the Scripting Games, you should be reviewing the Study Guide because it contains great information about essential tasks faced on a daily basis by network administrators.)<\/p>\n<p>Two topics to be covered in the 2012 Scripting Games are working with files and working with folders. NG, your question happens to hit both topics. At its most basic level, backing up modified files from a folder involves the following two tasks:<\/p>\n<ol>\n<li>Find all the files that have changed during a particular period of time.<\/li>\n<li>Copy the modified files to another location.<\/li>\n<\/ol>\n<h2>Find modified files<\/h2>\n<p>By using Windows PowerShell, this first task is incredibly simple; in fact, it is a one-liner. The one-line command to search a folder named C:\\data, and all of the folders contained inside that folder, for files that are written to today is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir c:\\data -r | ? {$_.lastwritetime -gt (get-date).date}<\/p>\n<p>Just a few notes about this command:<\/p>\n<ul>\n<li><b>Dir<\/b> is an alias for the <b>Get-ChildItem<\/b> cmdlet<\/li>\n<li><b>&ndash;r<\/b> is short for <b>-recurse<\/b><\/li>\n<li><b>?<\/b> is an alias for the <b>Where-Object<\/b> cmdlet<\/li>\n<li><b>(get-date).date<\/b> returns todays date as of midnight<\/li>\n<\/ul>\n<p>A longer and more readable version of the previous command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path c:\\data -Recurse | Where-Object { $_.lastwritetime -gt (get-date).date}<\/p>\n<p>The two commands are exactly the same. In the image that follows, the first command is the short version of the command, and the second command is the long version of the command. The output from each is the same.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4214.hsg-2-23-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4214.hsg-2-23-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One problem with the two previous commands is that if a file inside of a directory reports modification, the folder also reports as changed. For this particular scenario, NG is only interested in modified files, not the actual folders containing the files. Therefore, a change to the <b>Where<\/b><i> <\/i>clause needs to take place so that folders are filtered, but modified files remain. The first version (the short version of the command) is shown here with the necessary addition.<\/p>\n<p style=\"padding-left: 30px\">dir c:\\data -r | ? {!($_.psiscontainer) -AND $_.lastwritetime -gt (get-date).date}<\/p>\n<p>For ease of comparison, and to better illustrate the problem, the first command (returns folders and files) and the second command (returns only files) are shown in the figure that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6013.hsg-2-23-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6013.hsg-2-23-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><b>Note<\/b>&nbsp;&nbsp;&nbsp;For more information about using and accessing special folders, refer to <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/the-easy-way-to-use-powershell-to-work-with-special-folders\/\" target=\"_blank\">The Easy Way to Use PowerShell to Work with Special Folders<\/a><i>. <\/i>For more information about using Windows PowerShell to compress folders, refer to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/05\/22\/hey-scripting-guy-weekend-scripter-using-windows-powershell-to-compress-folders.aspx\" target=\"_blank\">Using Windows PowerShell to Compress Folders<\/a><i>.&nbsp; <\/i><\/p>\n<h2>Copy modified files to the network<\/h2>\n<p>To copy the modified files to the network is a rather easy task. I pipe the results of the command that obtains all the changed files to a <b>Foreach-Object <\/b>cmdlet, and then I use the <b>Copy-Item <\/b>cmdlet to copy the files to the network shared folder.<\/p>\n<p>Unfortunately, the <b>Copy-Item <\/b>cmdlet is not smart enough to accept the objects that are returned by the <b>Get-ChildItem <\/b>cmdlet as direct input. It will accept strings that represent paths to files as pipelined input, but not the results from the <b>Get-ChildItem<\/b>. When I see that a command is unable to accept pipelined input in the way I want to provide it, it nearly always means that I can accomplish what I want to do by using the <b>Foreach-Object <\/b>cmdlet. The two parameters used by the <b>Copy-Item <\/b>cmdlet are the <i>Path<\/i> to the original file, and a path to the destination file. The <i>Destination <\/i>parameter only needs the path to the folder, and not the complete file to the actual file name. The <b>%<\/b> symbol is an alias for the <b>Foreach-Object <\/b>cmdlet.<\/p>\n<p style=\"padding-left: 30px\">dir c:\\data -r | ? {!($_.psiscontainer) -AND $_.lastwritetime -gt (get-date).date} |<\/p>\n<p style=\"padding-left: 30px\">% {Copy-Item -path $_.fullname -destination \\\\hyperv1\\shared\\backup}<\/p>\n<p>If you decide that you would like to use the Windows Task Scheduler to schedule the previous command, see <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/use-scheduled-tasks-to-run-powershell-commands-on-windows\/\" target=\"_blank\">Use Scheduled Tasks to Run PowerShell Commands on Windows<\/a>.<\/p>\n<p>NG, that is all there is to using Windows PowerShell to back up a folder. Join me tomorrow for more Windows PowerShell 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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to back up modified files to the network. &nbsp;Hey, Scripting Guy! I have a folder that contains files in it, some of which I modify on a daily basis. I am wondering if I can use Windows PowerShell to back up only the modified files&mdash;those that have changed [&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":[38,11,3,185,12,45],"class_list":["post-11061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-shared-folders-and-mapped-drives","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to back up modified files to the network. &nbsp;Hey, Scripting Guy! I have a folder that contains files in it, some of which I modify on a daily basis. I am wondering if I can use Windows PowerShell to back up only the modified files&mdash;those that have changed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11061","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=11061"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11061\/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=11061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}