A Quick and Dirty Hyper-V Backup Script

I’ve replaced this script with a much more robust script that will backup each VM, including checkpoints/snapshots. You can find that post here and download the script from my TechNet profile.

Let’s celebrate the holiday season with a quick PowerShell script to backup a Hyper-V server…or maybe you just need a quick and dirty, belt and braces backup script for your Hyper-V based VMs.

It’s a small, quick script leveraging Hyper-V’s PowerShell module and some traditional command line utilities, like robocopy.

Get-VM | Stop-VM
ROBOCOPY /MIR /R:0 /W:0 /LOG:F:\vs11.log /NP /NDL F:\Hyper-V \\nas2\VM_Backups\vs11\Hyper-V
Start-VM wsus01
Start-VM mdt01

Let’s walk through the script.

Get-VM | Stop-VM

The first command gets a list of all the VMs on the host and then gracefully shuts them down, one after the other.


The script then waits for 20 seconds. This is for safety before it begins copying the VM files, and may not be necessary for you.

ROBOCOPY /MIR /R:0 /W:0 /LOG:F:\vs11.log /NP /NDL F:\Hyper-V \\nas2\VM_Backups\vs11\Hyper-V

Now we use Robocopy to copy the contents of the Hyper-V folder to the backup location, which in this example is on a networked attached storage box called nas2. Robocopy logs it’s actions to a log file in the root of the F:\ drive on the Hyper-V host.

Start-VM wsus01
Start-VM mdt01

The final section of the script starts the VMs up in a specified order with a time out in between each one. We do this instead of

Get-VM | Start-VM

because the “Start-VM” command won’t wait for each VM to boot before booting the next one, and with all the VMs on the host starting simultaneously the storage array will become overworked. The disadvantage of starting the VMs this way is that you’ll have to edit this script when adding new VMs. Another way of starting the VMs would be to restart the Hyper-V host after the backup and in the settings for each VM, set them to start automatically with a gradually increasing wait time for each one. See screenshot for an example.

Incremental delayed start-up for each VM

As usual, I recommend scheduling this script via Task Scheduler and having it run once a week. When scheduling a PowerShell script in Task Scheduler remember to use the following command line as the action:

PowerShell.exe -ExecutionPolicy Bypass F:\scripts\hyperv-backup.ps1

The script is designed to be run on each Hyper-V host locally, so you’ll want to space out the schedules to prevent each host from copying all it’s Hyper-V data to the one location at the same time and saturating the network and/or storage bandwidth.

I wrote this script with my small collection of Hyper-V hosts in mind. They all have the Hyper-V VM configuration and VHD files in the location F:\Hyper-V and have the relevant data backed up daily via the network-wide backup solution. This script runs once a week and is a belt and braces, extra backup for the VMs themselves. It does mean that there is some downtime for each of the VMs however. The advantage of having a copy of both the VHDs as well as the configuration files is that the VMs can be easily imported to another Hyper-V host and up and running very quickly.

I take great care to test my ideas and make sure my articles are accurate before posting, however mistakes do slip through sometimes. If you’d like to get in touch with me please use the comments, Twitter (you can tweet me and my DMs are open) or my contact form.

I hope this article helps you out, please consider supporting my work here. Thank you.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: