• 6 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 3/16/22

Foolproof and Schedule Your Scripts

 

Foolproof Your Script

To complete the backup script, we will wrap the Robocopy operation in a PowerShell failsafe routine (try) that allows the script to continue in the event of an error and send a message (catch) if something’s wrong.  

Use notepad.exe or your favorite text editor to create a PowerShell script called dobackup.ps1 - for example (from the command line):

notepad dobackup.ps1

Notes:

  • Specify your destination folder in place of E:\backup if it’s different.

  • The robocopy command is all one line - it’s line wrapped below.

  • The script also identifies your user name so that it can write the log file into the right documents folder. 

Enter the following text, including the “#” comments. These are to document your work for others who follow, or for you if you come back to the code some time later.

# My backup script
# Version 1.0
# Date 30-Dec-2021 
#
try{
robocopy C:\windows\fonts E:\backup /E
 /LOG:c:\Users\$env:USERNAME\Documents\backup.log
}
catch{
Write-Output "Error when running daily backup"
}

Once you have saved the script, you can try to run it by typing  .\dobackup  in PowerShell and pressing Enter. 

If you receive an error message stating that your script cannot be loaded because running scripts is disabled, you may need to permit the execution of local scripts by typing:

Set-ExecutionPolicy Remotesigned

in an admin PowerShell window and trying again.

If you get an error message with the Set-ExecutionPolicy command while using a company computer, refer to your IT department. 

For more information on the Set-ExecutionPolicy command, see this Microsoft article.

To verify that your backup has run, do a directory listing (1) and then check that the timestamp on your backup log is up to date (2):

The console screenshot illustrates the steps to verify your backup has run. Step one: run a directory listing command; dir Documents circled at top of the image. Step two: check the timestamp on your backup log is up to date; 30/12/2021 19:17 is circled.
Verify your backup has run with directory listing

If everything is working as expected, well done! You are now a PowerShell user! Many server automation and management tasks are done in PowerShell, so it’s a handy skill to have.

You can now close your PowerShell window, and we’ll go back to the Windows graphical user interface (GUI).

Automate Your Script

To run your script automatically, we need to ask the Windows Task Scheduler app to run it for us. Start by running the app in administrator mode; this step is very similar to how you start PowerShell: 

The screenshot shows how to run the task scheduler in administrator mode. Step one: on the left bottom end corner, in the search bar, type scheduler. Step two: on the tab open on the right, select Run as Administrator from the list.
Run Task Scheduler in administrator mode

We need to create a new task:

The screenshot shows how to create a new task in task scheduler. Be on Task scheduler (Local) as shown in the left column; then be on the Actions tab as shown on the right side of the image. From the list, Create task is selected.
Select Create Task in Task Scheduler

Fill in the required information on each tab and only click OK to save the created task when you’ve completed everything highlighted in the image below: 

Task scheduler parameter window. At the top, different tabs can be opened; the General tap is open. Name field: Do my backup. Description: Runs my backup script. Security options: Run whether user is logged on or not. Configure for Windows 10.
Set up new task parameters

Adapt the Frequency of Your Backup

A calendar represents the frequency of the backups.
Adapt the frequency of your backup

Now we need to define how often we want the script to run:

  1. Click on the Triggers tab.

  2. Click on New.

  3. Set the start date and time.

  4. Set the frequency of your backup script (every day).

  5. Tick the Enabled box and click OK.

Task scheduler parameters window. The triggers tab is open. On the left the button New is highlighted. A new trigger window is open on the right. Daily is selected from the Settings; date, time and recurrency are set. Enabled is ticked in Advance settings
Adapt the backup script frequency

Here, create a new trigger for the script and set it to run daily at 17:30 (5:30 pm).

Finally, we need to define what happens when the task is triggered; we want PowerShell to start and run our script. Note how this information is entered:

  1. Click on the Actions tab.

  2. Click on New.

  3. In the Action dropdown menu “Start a program” is set by default. Keep it.

  4. Select the program/script field using Browse. Here you need to select  C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe

  5. In “Add arguments,” you will have to specify the script path. In our example: -file C:\Users\User\dobackup.ps1 

All user account home folders are located under C:\Users on a standard Windows installation. Our example user is called User, hence that name is seen in the above command. If you are trying out this setup, use your login name here instead.

Task scheduler parameters window. The Actions tab is open. On the left, the button New is highlighted. The Edit Action window is open on the right. Action: start a program; program/script is filled using Browse. In Argument the script path is specified.
Set up the recurrent task parameters

Finally, click OK to close the Edit Actions window and click OK again to close the Create Task operation. 

You will now be prompted to re-enter your password to confirm the task has authority to run and that you have authority to create the task:

The screenshot shows the window that opens after closing the create task window. In this window, you are prompted to enter user account information for running this task: username and password.
Confirm authority to run the task

From the Task Scheduler Library you can run your script manually at any time by selecting it (1) and clicking the Run action (2). Try it now! 

The screenshot shows a list of tasks on the left, organised by Name, status and triggers. The first item on the list Do my backup is selected. An Actions tab is opened on the right; under Selected item, the option Run is selected.
Run the script manually

Now use Windows File Explorer to check the timestamp of the log file, which should be up to date if the script completed correctly. Double-click the filename, and you should see a report about your latest backup.

Try running the backup manually again to confirm the timestamp changes! Double-checking is always safer!

If the script seems to be running for a long time, click the Refresh button in the right panel to update the status. 

Well done, you have created a scheduled copy/backup task!

Modifying the original PowerShell script would allow other folders to be backed up, such as My Documents, picture folders, etc. Similarly, you could change the destination from a USB flash stick to an external disk drive or shared network folder.

If you are interested in learning more about PowerShell and Robocopy, check out this article. Here you can find some good examples of scripts. 

Let’s Review it in a Video

Review the different steps you saw in this chapter in the video below:

Let’s Recap!

  • To run your Robocopy command with some error handling, you can wrap it in a script file.

  • You can arrange for the Windows Scheduler to run your scripted backup according to your specified schedule.

  • Having the script write a log file helps check that it’s running as expected because you can check the log file’s timestamp and contents. 

You now know how to create PowerShell Scripts and automate them! In the next chapter, you’ll see how to use the Windows graphical application EaseUS to set up and manage backups. Let’s go!

Example of certificate of achievement
Example of certificate of achievement