Using Windows Powershell Back Up Specific Folders
Hello
My friends often ask me this question
Is there anyway I can set Windows Vista to only backup folders of my choice?
Here is a guide to do this. Why use third party programs.
BackupFolderToServer.ps1
Code:
param($source, $destination, $help)
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: BackupFolderToServer.ps1
Backes up files in a folder to a mapped drive. The destination
folder does not have to be present
PARAMETERS:
-source the source of the files and folders
-destination where the files are to be copied
-help prints help file
SYNTAX:
BackupFolderToServer.ps1 -source c:\fso -destination h:\fso
Backs up all files and folders in c:\fso on local machine to
a mapped drive called h. The \fso folder does not need to
exist on the h:\ drive.
BackupFolderToServer.ps1
generates an error. the -source and -destination parameters
must be present
BackupFolderToServer.ps1 -help ?
Displays the help topic for the script
"@
$helpText
exit
}
if($help){ "Obtaining help ..." ; funhelp }
if(!$source -or !$destination)
{
$(throw "You must supply both source and destination.
Try this BackupFolderToServer.ps1 -help -?")
}
Copy-Item -Path $source -destination $destination -recurse
Re: How to Back Up Specific Folders on My Windows Computer?
In the BackupFolderToServer.ps1 script, we use the Copy-Item 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’s mapped home directory. The BackupFolderToServer.ps1 script begins with the param 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 “driven” 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: -source, -destination, and -help. Each of these parameters will be stored in the corresponding variable with the same name. This line of code is seen here:
Code:
param($source, $destination, $help)
We then create the funhelp function. The funhelp function is used to display a Help text message when the script is run with the -help parameter. The funhelp function begins with a declaration of the $helpText variable. This variable will be used to store a here-string. The here-string 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 $helpText variable on the screen and exit the script. The completed funhelp function is seen here:
Code:
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: BackupFolderToServer.ps1
Backes up files in a folder to a mapped drive. The destination
folder does not have to be present
PARAMETERS:
-source the source of the files and folders
-destination where the files are to be copied
-help prints help file
SYNTAX:
BackupFolderToServer.ps1 -source c:\fso -destination h:\fso
Backs up all files and folders in c:\fso on local machine to
a mapped drive called h. The \fso folder does not need to
exist on the h:\ drive.
BackupFolderToServer.ps1
generates an error. the -source and -destination parameters
must be present
BackupFolderToServer.ps1 -help ?
Displays the help topic for the script
"@
$helpText
exit
}
Re: How to Back Up Specific Folders on My Windows Computer?
We now need to check for the presence of the $help variable. If we find the $help variable, we display a progress message and call the funhelp function. The semicolon allows us to run two separate commands on the same line of text. This command is seen here:
Code:
if($help){ "Obtaining help ..." ; funhelp }
We need to look for the presence of the two mandatory parameters. These parameters are the -source and the -destination 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 throw statement to display an error message and exit the script. This error is seen here:
http://gallery.techarena.in/data/513/backup.jpg
In the error that appears, we point the user to the Help text syntax. The code that does this is seen here:
Code:
if(!$source -or !$destination)
{
$(throw "You must supply both source and destination.
Try this BackupFolderToServer.ps1 -help -?")
}
Now it is time to copy the files. The Copy-Item cmdlet accepts the -path parameter that is contained in the $source variable. We use the $destination variable to feed the -destination parameter, and we use the -recurse switch to cause nested folders to also be copied. The source and destination 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:
Code:
Copy-Item -Path $source -destination $destination -recurse
I hope you will be able to use the BackUpFolderToServer.ps1 script.