{"id":53623,"date":"2009-05-31T23:37:00","date_gmt":"2009-05-31T23:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/31\/hey-scripting-guy-how-can-i-back-up-specific-folders-on-my-windows-computer\/"},"modified":"2009-05-31T23:37:00","modified_gmt":"2009-05-31T23:37:00","slug":"hey-scripting-guy-how-can-i-back-up-specific-folders-on-my-windows-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-back-up-specific-folders-on-my-windows-computer\/","title":{"rendered":"Hey, Scripting Guy! How Can I Back Up Specific Folders on My Windows Computer?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" 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\"> <\/H2>\n<P>Hey, Scripting Guy! I would like to be able to perform a backup of only certain folders on my Windows Vista laptop. I am concerned about running through the airport and having it getting bumped, dropped, or stolen. My job requires me to travel extensively, and I am just paranoid about a laptop crash and about losing my data. Can you help me?<BR><BR>&#8211; PC<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" 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\"> \n<P>Hi PC,<\/P>\n<P>Scripting Guy Ed agrees with you and shares your concerns. Wait while he goes and gets a cup of tea. He is out in Redmond, Washington, in the United States this week teaching a group of Microsoft engineers Windows PowerShell. In honor of his presence, it has been sunny with clear skies all week long. <\/P>\n<P>One time a couple of years ago, while Ed was traveling to Quebec City, the airplane he was riding in hit what felt like a series of speed bumps in the sky, and his cup of hot coffee went onto his laptop. Because his laptop was not java enabled, it killed the laptop. (Interestingly enough, this is about the same time that Ed quit drinking coffee.) He lost two days of work on his Windows PowerShell book and became depressed for a week. After he arrived at the Microsoft office in Quebec City, he was able to get the laptop repaired (luckily, it was only the keyboard that was damaged, and the remainder of the laptop, including the data on the hard drive, emerged unscathed). When his laptop was repaired and running, he wrote the <B>BackupFolderToServer.ps1<\/B> script you see just below. It has since served him well. We hope you will also find it useful, too.<\/P>\n<P><B>BackupFolderToServer.ps1<\/B><\/P><PRE class=\"codeSample\">param($source, $destination, $help)<\/p>\n<p>function funHelp()\n{\n$helpText=@&#8221;\nDESCRIPTION:\nNAME: BackupFolderToServer.ps1 \nBackes up files in a folder to a mapped drive. The destination\nfolder does not have to be present<\/p>\n<p>PARAMETERS: \n-source      the source of the files and folders\n-destination where the files are to be copied\n-help        prints help file<\/p>\n<p>SYNTAX:\nBackupFolderToServer.ps1 -source c:\\fso -destination h:\\fso<\/p>\n<p>Backs up all files and folders in c:\\fso on local machine to\na mapped drive called h. The \\fso folder does not need to \nexist on the h:\\ drive.<\/p>\n<p>BackupFolderToServer.ps1 <\/p>\n<p>generates an error. the -source and -destination parameters\nmust be present<\/p>\n<p>BackupFolderToServer.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&#8220;@\n$helpText\nexit\n}<\/p>\n<p>if($help){ &#8220;Obtaining help &#8230;&#8221; ; funhelp }\nif(!$source -or !$destination) \n  { \n    $(throw &#8220;You must supply both source and destination.\n Try this BackupFolderToServer.ps1 -help -?&#8221;) \n  }\nCopy-Item -Path $source -destination $destination \u2013recurse\n<\/PRE>\n<P>In the <B>BackupFolderToServer.ps1<\/B> script, we use the <B>Copy-Item<\/B> cmdlet to copy files in a particular folder to a mapped drive location on a server or some other device on which you wish to store your user data. This could be used to copy data to a portable storage device such as a flash memory card or a USB drive. It will also copy files to a user\u2019s mapped home directory. The <B>BackupFolderToServer.ps1<\/B> script begins with the <B>param<\/B> statement, which allows us to specify command-line arguments to the script. These arguments control how the script will run. This also saves the trouble of having to edit the script before using it. Such a script can be \u201cdriven\u201d from a batch file that supplies a number of parameters. It can also be called from other Windows PowerShell scripts. This script defines three parameters: <B>\u2013source<\/B>, <B>\u2013destination<\/B>, and \u2013help. Each of these parameters will be stored in the corresponding variable with the same name. This line of code is seen here: <\/P><PRE class=\"codeSample\">param($source, $destination, $help)\n<\/PRE>\n<P>We then create the <B>funhelp<\/B> function. The <B>funhelp<\/B> function is used to display a Help text message when the script is run with the <B>\u2013help<\/B> parameter. The <B>funhelp<\/B> function begins with a declaration of the <B>$helpText<\/B> variable. This variable will be used to store a <B>here-string<\/B>. The <B>here-string<\/B> is used to allow us to type text in the manner in which it will be displayed on the screen. This saves time and reduces potential quoting errors. The Help text consists of three sections: the description of the script, the parameters the script will accept, and the syntax that is required. AFter the Help text is created, we display the contents of the <B>$helpText<\/B> variable on the screen and exit the script. The completed <B>funhelp<\/B> function is seen here:<\/P><PRE class=\"codeSample\">function funHelp()\n{\n$helpText=@&#8221;\nDESCRIPTION:\nNAME: BackupFolderToServer.ps1 \nBackes up files in a folder to a mapped drive. The destination\nfolder does not have to be present<\/p>\n<p>PARAMETERS: \n-source      the source of the files and folders\n-destination where the files are to be copied\n-help        prints help file<\/p>\n<p>SYNTAX:\nBackupFolderToServer.ps1 -source c:\\fso -destination h:\\fso<\/p>\n<p>Backs up all files and folders in c:\\fso on local machine to\na mapped drive called h. The \\fso folder does not need to \nexist on the h:\\ drive.<\/p>\n<p>BackupFolderToServer.ps1 <\/p>\n<p>generates an error. the -source and -destination parameters\nmust be present<\/p>\n<p>BackupFolderToServer.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&#8220;@\n$helpText\nexit\n}\n<\/PRE>\n<P>We now need to check for the presence of the <B>$help<\/B> variable. If we find the <B>$help<\/B> variable, we display a progress message and call the <B>funhelp<\/B> function. The semicolon allows us to run two separate commands on the same line of text. This command is seen here:<\/P><PRE class=\"codeSample\">if($help){ &#8220;Obtaining help &#8230;&#8221; ; funhelp }\n<\/PRE>\n<P>We need to look for the presence of the two mandatory parameters. These parameters are the <B>\u2013source<\/B> and the <B>\u2013destination<\/B> parameters. The source is a local path that must exist on the machine and for which you have rights to the folder. The destination does not have to have a folder that is the same name as the source. The destination can be a UNC path, a mapped network drive, a local drive, or even another folder on the same drive as the source. If these two parameters do not exist, we use the <B>throw<\/B> statement to display an error message and exit the script. This error is seen here:<\/P><IMG border=\"0\" alt=\"Image of the error displayed by the throw statement\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/june\/hey0601\/hsg-06-01-09-01.jpg\" width=\"500\" height=\"322\"> \n<P>&nbsp;<\/P>\n<P>In the error that appears, we point the user to the Help text syntax. The code that does this is seen here: <\/P><PRE class=\"codeSample\">if(!$source -or !$destination) \n  { \n    $(throw &#8220;You must supply both source and destination.\n Try this BackupFolderToServer.ps1 -help -?&#8221;) \n  }\n<\/PRE>\n<P>Now it is time to copy the files. The <B>Copy-Item<\/B> cmdlet accepts the <B>\u2013path<\/B> parameter that is contained in the <B>$source<\/B> variable. We use the <B>$destination<\/B> variable to feed the <B>\u2013destination<\/B> parameter, and we use the <B>\u2013recurse<\/B> switch to cause nested folders to also be copied. The <B>source<\/B> and <B>destination<\/B> variables are created via the parameters of the same name from the command line when the script is run. This line of code is seen here:<\/P><PRE class=\"codeSample\">Copy-Item -Path $source -destination $destination \u2013recurse\n<\/PRE>\n<P>Well, PC, Ed has finished his cup of tea (a nice aromatic mint tea). We hope you will be able to use the <B>BackUpFolderToServer.ps1<\/B> script. Ed keeps a 4 GB SD chip in his laptop while he is traveling, and he uses <B>BackupFolderToServer.ps1<\/B> to copy his working files to the SD chip about once each hour. Join us tomorrow as we continue to look at managing user data. We also hope you are getting in shape for the Summer Scripting games. Warmups begin next week. For all the details, follow us <A href=\"https:\/\/twitter.com\/scriptingguys\/\" target=\"_blank\">on Twitter<\/A>. Until tomorrow, peace!<\/P>\n<P>&nbsp;<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I would like to be able to perform a backup of only certain folders on my Windows Vista laptop. I am concerned about running through the airport and having it getting bumped, dropped, or stolen. My job requires me to travel extensively, and I am just paranoid about a laptop crash and [&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":[343,423,3,45],"class_list":["post-53623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-backup","tag-backup-and-system-restore","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I would like to be able to perform a backup of only certain folders on my Windows Vista laptop. I am concerned about running through the airport and having it getting bumped, dropped, or stolen. My job requires me to travel extensively, and I am just paranoid about a laptop crash and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53623","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=53623"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53623\/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=53623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}