08 February 2013

Using Powershell to Backup Hyper-V VMs - Step by Step

Server 2012 has load of improvements for backup.  The ability to backup a running virtual machine without any interruption or downtime is really powerful.  Automating the backup task creation for virtual machines on a Hyper-V host will save you loads of time once you have tuned your script.

The 40 something Windows Server Backup cmdlets are very well documented http://technet.microsoft.com/en-us/library/jj902459.aspx  but they lack a full example of how to put everything together.  NOTE:  Additional cmdlets were added after 2008 R2 and I am using there here, so this is for Server 2012 only.

The process below is how I setup a script to backup select virtual machines on a secondary volume of a single disk server.  I will step through how to change these options so dont worry of this does not exactly fit your requirement.

First up a few basics.  A backup job is called a backup policy.  You can only have one backup policy.  You can only backup to certain locations, as an example these exclude external USB drives.

The policy contains the various settings or parameters, such as schedule and what to back up.  The various cmdlets let you configure these

What you need

  • A new policy (WBPolicy)
  • A backup destination (WBBackupTarget)
  • A list Virtual Machines to backup (WBVirtualMachine)
  • A Backup schedule / times (WBSchedule)


Step 1 Create a new policy
There can only be one policy so if there is an existing one you need to remove it

   Remove-WBPolicy -all -force

Now you can create your new policy to which we will add the various options

   $BackupJob = New-WBPolicy

Step 2 Specify a backup destination
This allows you to specidy a seperate "drive" that is visible to the OS. this can be on a separate physical disk or an additional partition on the OS physical disk.

   $BackupTarget = New-WBBackupTarget -VolumePath B:
   Add-WBBackupTarget -Policy $BackupJob -Target $BackupTarget


Step 3 Add the virtual machines
You can specify which machines to add, or add all of them. I have opted to only backup VMs that contain "ET-LAB" in the name

   $VMs = Get-WBVirtualMachine | where vmname -like "et-lab*"
   Add-WBVirtualMachine -Policy $BackupJob -VirtualMachine $VMs

To add all VMs you can just specify

   Add-WBVirtualMachine -Policy $BackupJob -all

Step 4 Specify a schedule
The backups will run daily, you just really need to specify at what time it should back up.  I have specified to backup at 10AM and 10PM daily


   $BackupTime = [datetime] "10:00", [datetime] "22:00"
  Set-WBSchedule -Policy $BackupJob -Schedule $BackupTime



Step 5 Activate the policy
By now the policy caonts everythign it needs to be able to run.  You now need to activate it.

   Set-WBPolicy -Policy $BackupJob -AllowDeleteOldBackups


Step 6 Manually start a trial job
Once your job has been configured you can use the Windows Server Backup GUI to start things or you can also start it from Powershell

   Start-WBBackup (Get-WBPolicy)

The job should now start of and you should see the following:


Looking at the Hyper-V console you will see that a active backup is indicated in the status column




You can put all these commands together in a PowerShell script that you can run on multiple computers.  Just remember to set the execution policy before you try to run the script

  Set-ExecutionPolicy -ExecutionPolicy Unrestricted


My complete script looks as follows


#Remove Existing Backup Jobs
try { Remove-WBPolicy -all -force }
Catch { Write-Host "No existing Jobs to remove"}

# Create new empty Policy
$BackupJob = New-WBPolicy

# Create the backup target $BackupTarget
# Using a volume

$BackupTarget = New-WBBackupTarget -VolumePath B:

# Add the backup target to the policy

Add-WBBackupTarget -Policy $BackupJob -Target $BackupTarget

#  Add the list of Virtual machines

$VMs = Get-WBVirtualMachine | where vmname -like "et-lab*"

Add-WBVirtualMachine -Policy $BackupJob -VirtualMachine $VMs

# Setup the scheduled backup times

$BackupTime = [datetime] "10:00", [datetime] "22:00"

Set-WBSchedule -Policy $BackupJob -Schedule $BackupTime


# Activate the policy

Set-WBPolicy -Policy $BackupJob -AllowDeleteOldBackups