• 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

Write Your Backup Script With PowerShell

Get Ready to Script With PowerShell

For our exercise, we’ll use your Windows machine as if it was a PiraSTEM workstation, and we will perform a backup to simulated server storage - this can either be another folder on your machine or a USB memory stick.

If your desktop PC is running Linux or you have an Apple computer and no access to a machine running Windows, you could consider setting up a virtual Windows machine; you might find part 2 of the course Set Up Virtual Machines Using VirtualBox and vSphere useful.

PowerShell is a scripting language for executing command-line instructions; it’s installed by default on Microsoft operating systems, so there is no need to download it!

In our case, we will be using PowerShell to create a script to run the Robocopy utility. Robocopy is a handy, command-line tool for making simple copies of folders on Windows-based computers and can form part of a larger backup scheme.  

Creating the script for Robocopy will achieve the following objectives:

  • Make a copy of the specified folders and their files.

  • Create a log file of the backup activity.

  • Have the script run at a defined time or period (we’ll hold on to this for another chapter!).

    Left to right the three chronological steps to set up a backup solutions are illustrated by: a page representing the identification of what to backup; a location pin representing where to send the backups; and a calendar representing  the backup frequency
    The three steps to set up a backup solution

Identify What You Want to Backup

PiraSTEM has essential data files (mostly large multimedia and electronic circuit design files) kept on local workstations to speed up processing. This data will need backing up. Instead of installing a backup application on each workstation, they decided that they could write a simple PowerShell script to copy the important files to one of their servers. They would then be captured by the daily backups and thus join their 3-2-1 scheme.

A page represents the identification of what data to backup.
Identify what to backup

Let’s start by creating the script instructing Robocopy to make copies of specified folders. The first time we do a backup, Robocopy makes a full copy; it then just copies the differences. It’s not the same as having GFS-type differential backups because you only have one updated full backup.

We will develop and test the script in a PowerShell command window to gain full access (for backing up) to all the files and folders on a Windows machine.

Watch the video below to see how to start PowerShell and the syntax for describing file and folder locations on the command line.

To use Robocopy, we need to open a PowerShell window in Administrator mode; this gives the command the right level of privileges to run properly. This takes two steps:

  1. Type “PowerShell” into your Windows search tool, or press the Windows key and type it “blind.” Either way, a Window similar to the following will appear.

  2. Select the “Run as Administrator” option on the right. 

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

When you run PowerShell as the administrator, it starts up in the Windows System32 folder, but you should be working in your user’s home folder.

To change the directory back to where you want to be, and if you know your username, type the command:

cd \users\<username> 

If, however, you are not sure, you can let Windows work things out with the command:

cd \users\$env:username

The PowerShell prompt should then confirm you are in the right place.

PowerShell scripts are all text-based, so we have to type in path names to refer to files and folders, and of course, that’s rather important when we’re doing backups!

Clarify Where You Want to Send Your Backup

A location pin represent where to send the backups.
Choose where to send your backup

Robocopy can send a backup to any location recognizable by Windows; that could be external storage (flash, disk, etc.) or across a network to a server or storage attached directly to the server. Robocopy does not have any concept of tape systems, so we cannot use it to backup workstations directly to tape. We could use Robocopy to send data to a server where a tape backup program could pick it up.

PiraSTEM was looking at in-office storage options (tape or disk) as the immediate location for their backups. You’ll use your USB flash drive to simulate this storage for this exercise. Make sure there’s at least 1GB of free space on your USB stick.

Try a simple copy command. Insert your target USB flash drive and use File Explorer to identify its drive letter; for example, here it’s called “USB-STICK” and it’s Windows drive E:

A list of files in File explorer is shown. At the end of the list the file USB-STICK (E:) is circled to show where to find the drive letter of your USB stick. The drive letter is between brackets.
Identifying USB-STICK drive letter

See how to send your backup to your USB key in the next video:

The command line used to send your backup to your USB-STICK is:

robocopy c:\windows\fonts e:\backup /E 

Where  c:\windows\fonts  is the source (what we are backing up) and  e:\backup /  E is our destination. 

Remember that the /E option is needed to back up empty subfolders, and if you leave this off, your backups can be incomplete. 

What if I want to check my backup outside the PowerShell console? 

You need to append the option  /LOG:logfilename.txt ! This will create a text log file with all the commands and outputs shown in the PowerShell console. The complete command line is:

robocopy c:\windows\fonts e:\backup /E /LOG:log.txt 

You’re giving the file name “log” to this log file.

Let’s Recap!

  • Using PowerShell, you can run commands such as Robocopy, manually, when needed.

  • Your computer needs to be run in administrator mode to allow PowerShell’s commands to have full access.

  • PowerShell in administrator mode can adjust computer settings and modify almost any file, so make sure you know what you are doing and check your command syntax carefully!

  • To ensure that your backed-up folder structure is complete, always use Robocopy with the /E option to copy empty folders. 

Great! We identified what to backup, made copies of it, and sent them to our preferred location. But remember, you don’t have a valid backup unless you have verified that you can get the data back when needed! Let’s see how to test that our backup works in the next chapter! 

Example of certificate of achievement
Example of certificate of achievement