I bet you didn't know that Windows Server 2012 R2 with Hyper-V lets you export your virtual machines -- wait for it -- while they are running. In prior Windows Server versions, you could export VMs, but you had to shut them down first, which meant your virtual server's applications were down, too.

You might export your live VMs to grab a quick backup of VMs or to archive your VMs because the export option contains the entirety of the virtual machine, including configuration information, for use as a quick-and-easy import for other servers. For example, you could use this feature to quickly create a parallel environment in a lab sandbox for troubleshooting and testing.

[ Read J. Peter Bruzzese's primer on using PowerShell. | Stay atop key Microsoft technologies in InfoWorld's Technology: Microsoft newsletter. ]

The export process is simple: Selecting your VMs, right-click, choose Export from the contextual menu that appears, and provide the path for the exported VMs. Yes, you can use a network share for your location or plop it into a cloud-based folder like Dropbox.

What if you want to have this all happen automatically? Automation in Windows Server means using PowerShell, so I started looking for a PowerShell cmdlet to make the export happen. It exists, and logically enough, it's called Export-VM. If you want to export a VM named DC1 to a folder on the F: drive called Backup, you would type Export-VM -Name DC1 -Path F:\Backup .

If you want to export all the VMs on the system, you would type: Get-VM | Export-VM -Path F:\Backup . You might even put that command into Notepad and save it as VMBackup.ps1 (the .ps1 filename extension is necessary for Windows Server to recognize it as a PowerShell script), so you could run it easily again. If you saved the PowerShell script to a folder called Scripts in the C: drive, you could just navigate to the folder and type .\VMBackup.ps1 to execute that VM export. Or you could schedule your VM export script with the Task Scheduler.

Why stop there? What if you wanted a daily or weekly export, plus a monthly export for offsite backup? I kicked this idea around, and once I realized I was in over my head, I contacted my good friend Jeff Hicks, a multiyear Microsoft MVP in PowerShell. I wanted to start simple, with four weekly backups each month and a monthly report to be stored in a Dropbox folder. I wanted the script to create the folders using time stamps and to delete any folders older than four versions ago, so only the latest four backups are kept on premises. For the monthly backup archived to Dropbox, I wanted to keep only the last two: the current month's and the previous month's.

I expected this to be a one-line script, but I was naive. Hicks came back to me with an amazing script that he's allowing me to share with everyone. They don't call him Professor PowerShell for nothing! (That script is saved as a .txt file so as not to trigger antimalware, so be sure to change the filename extension to .ps1 after downloading or saving it so that Windows Server sees it as a PowerShell script.)

The script creates the folders, checking for existing ones and deleting the older ones, and exports the VMs. Note that you need to indicate the location where you want the exported VMs to be placed (in the current script, they are sent to c:\work\export).

In the script, Hicks notes, "I'm using a technique called 'splatting' that passes a hashtable of parameters to Export-VM, which makes the code easier to read. And because exporting can take a long time, the Export-VM cmdlet has an –AsJob parameter that creates a PowerShell background job. My script allows you to pass that on. Otherwise, PowerShell will wait until the exports are finished before you get your prompt back."

Hicks went one step further and wrote up a PowerShell scheduled job to execute the script (located at the bottom of the script as a guide for you to create your own). I was curious why he took this direction when he could have expected admins to use scheduled tasks (which can also be created using PowerShell). The reason: Some features in scheduled jobs aren't available in scheduled tasks. When the output is important, the better choice is the scheduled job. You can find the scheduled jobs at Microsoft\Windows\PowerShell\ScheduledJobs .

I'm pretty happy with the export options for running VMs in Windows Server 2012 R2, and I'm even happier that I've had a chance to play with PowerShell, build some simple commands and scripts, and benefit from Hicks' expertise to get even better ones to share with you.

If you have any solid examples you would like to share with other readers, please discuss them in the comments.

This story, "Two tricks to automate the export of live VMs in Windows Server," was originally published at InfoWorld.com. Read more of J. Peter Bruzzese's Enterprise Windows blog and follow the latest developments in Windows at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.