Export multiple Windows Server 2012 R2 Hyper-V Guests with PowerShell Part I

After spending many hours building out multiple Hyper-V guests there’s always a chance that something will go wrong and you’ll be needing a backup to restore from.  There are many options available, but suffice it to say that while useful, Checkpoints & Replication are not backups.

So what are the other options?  The simplest is an Hyper-V Export to another location.  The more robust solution is using System Center 2012 R2 Data Protection Manager, [which I’ll begin to cover in Part III].

Now, while we can manually Export… any guest from Hyper-V Manager wouldn’t it be nice to be able to query the list of machines and then Export… them one by one to a location on an automated basis using Task Scheduler?

Before we get into the PowerShell to accomplish this task, I’d like to point out that Microsoft still has not provided an –Overwrite switch on the Export-VM command.  Thus if you want to repeat this process and keep multiple weeks or a months backups you’ll have to find another option for if the files/folders exist the Export-VM command will fail to execute.  So what’s a possible solution?  Create a folder with a current data/time stamp to hold all of the exported machines and then do this each time the PowerShell command executes.  Simple right?  Well, keep in mind that after awhile these folders will pile up and you’ll also need a method of deleting old ones as well depending upon how long you want to store them. 


In the example above a separate drive D: has been designated as the Export location and a folder called Exports has been created to store each export based upon the data/time the Task Scheduler task executes the PowerShell script.

The first step in the PowerShell script is to query the list of existing folders and then removes the folder older than one month from the current date/time.  This keeps 5 backups and helps reduce the disk space required for the backups.  [NOTE:  System Center 2012 R2 Data Protection Manager can significantly reduce backup disk space consumption, but we’ll cover that in Part III.]

So let’s look at a simple script to accomplish the above, with a single machine, then we’ll get fancy (in Part II) skirting some other issues that result from exporting multiple machines at one time and the disk I/O, we’ll also address the SLA issue of 99.9% availability for the machines you want to backup, etc.

First the assumptions:

  • We have only one guest machine we want to export/backup.
  • It is OK to stop the guest machine while we back it up and then we’ll restart it after the export is complete.
  • It only takes 3 minutes to shutdown the guest machine properly.

The simple PowerShell script below will accomplish this task given the assumptions above.


# $RootPath: Can be changed to point where you’d like the Virtual Machines exported and can be any valid

#            drive path.

$RootPath = ‘D:\Exports\’

# $OutputDate: Is simply used to create a unique folder name that later can have it’s CreationDate checked

#              for deletion later.

$OutputDate = (Get-Date).Year.ToString() + ‘.’ + (Get-Date).Month.ToString() + ‘.’ + (Get-Date).Day.ToString() + ‘.’ + (Get-Date).Hour.ToString() + ‘.’ + (Get-Date).Minute.ToString() + ‘.’ + (Get-Date).Second.ToString()


$GuestVMName = “GuestVirtualMachineName”




    Stop-VM -Name $GuestVMName

    Start-Sleep -Seconds 180








#Clean up old Export-VM folders older than one month ago to reduce disk space consumption.

foreach ($i in Get-ChildItem $RootPath)


    if ($i.CreationTime -lt ($(Get-Date).AddMonths(-1)))


            #Write-Output $i.Name.ToString()

            Remove-Item $i.PSPath -Recurse




#Create new Export folder with current date/time.

$ExportPath = New-Item ($RootPath + ‘ ‘ + $OutputDate) -type directory


#Export the VM.

Export-VM -ComputerName $env:COMPUTERNAME -Name $GuestVMName -Path $ExportPath


#Start the VM.



    Start-Sleep -Seconds 180

    Start-VM -Name $GuestVMName








Adding this PowerShell to Task Scheduler and setting up a schedule provides an easy way to backup this single guest machine on a regular basis.

In Part II of this series, we’ll get into a more real world example of how to accomplish this for multiple guests and ensure that we’re exporting only one machine at a time to ensure quality disk I/O on the destination disk.  In Part III, we’ll get into System Center 2012 R2 Data Protection Manager.

Tagged with:
Posted in Hyper-V, PowerShell, Windows Server 2012 R2

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

%d bloggers like this: