Backup and restore of Windows Azure virtual machines has been a hot topic lately in the events that I’ve been delivering.  A few days ago, I published Part 1 of a two-part series on this topic on the Hey, Scripting Guy! blog that covers the first part of the process: safely backing up Windows Azure virtual machine hard disks to cloud-based blob storage.


This article is Part 2 in that series and focuses on completing the end-to-end recovery process by restoring Windows Azure virtual machines from cloud-based backup locations.

  Interested in learning more about Disaster Recovery?

  Check out our complete blog series at:

Before you begin …

To be successful in following the steps in this article, you’ll need to have first performed the following tasks:

  1. Activate a FREE Windows Azure Trial Subscription.
    You can quickly sign-up for a FREE Windows Azure Trial Subscription at
  2. Provision at least one virtual machine in Windows Azure.
    You can walk through the steps involved in building an on-demand cloud-based lab environment in Windows Azure at
  3. Create a backup of at least one virtual machine in Windows Azure.
    You can quickly create a VM backup on Windows Azure using the steps in the article located at


Let’s Get Started!

Windows PowerShell and the free Windows Azure PowerShell Module can be used to automate cloud-based backup and restore processes with the Start-AzureStorageBlobCopy cmdlet.  This cmdlet performs “blob-to-blob” copies up in the cloud, giving you the power to quickly copy large amounts of data without pushing this data back-and-forth over your Internet connection. Using this cmdlet, you’ll be able to backup and restore entire virtual machine hard disks in a few minutes or less!

To restore Windows Azure virtual machines, we’ll step through the following tasks in this article:

  • Select Virtual Machine to Restore
  • Identify Each Virtual Hard Disk to be Restored
  • Deprovision Virtual Machine to Restore
  • Restore Windows Azure VM OS Disk
  • Restore Windows Azure VM Data Disks  
  • Reprovision Virtual Machine

Tip! To learn more about the basics of Windows Azure Infrastructure Services, you may also be interested in the “Early Experts” Cloud Quest and our scenario-based “Cloud Labs” step-by-step guides – both are FREE online study resources that provide hands-on lab exercises for leveraging Windows Azure and building key IT Pro cloud scenarios.

Select Virtual Machine to Restore

The Virtual Machine you wish to restore from a prior backup can be selected by using the Get-AzureVM cmdlet. Running Get-AzureVM alone returns a list of Virtual Machines currently provisioned in Windows Azure.



To select a particular virtual machine, you can pass the ServiceName and Name values as parameters and set the output to a new PowerShell variable.

$vm = Get-AzureVM –ServiceName kemlabad –Name kemlabad01

Now, our selected Windows Azure virtual machine can be referenced by using the variable $vmin the remainder of our script.

In order to restore a backup copy of each virtual machine hard disk, we also need to temporarily shutdown the virtual machine into a state where the VM is not running, but its configuration is kept in a provisioned state. We can accomplish this with the Stop-AzureVM cmdlet.

$vm | Stop-AzureVM –StayProvisioned


Our virtual machine is selected and in the right state, and we can proceed to the next step of finding each virtual hard disk we wish to restore.

Identify Each Virtual Hard Disk to be Restored

Windows Azure virtual machines can be provisioned with two general types of virtual hard disks: OS Disks and Data Disks. Each VM will have one OS Disk from which it boots and runs the operating system. In addition, each VM can also have one or more additional Data Disks on which program code and data files can be stored. To perform a complete VM restore, we’ll need to identify all of the virtual hard disks that our VM is currently using.

To store the location for the OS Disk, we can use the Get-AzureOSDisk cmdlet.

$vmOSDisk = $vm | Get-AzureOSDisk

For any virtual hard disk that we wish to backup or restore, the two property values in which we’ll be most interested are the DiskName and MediaLinkvalues shown below. These values provide the information we’ll need to properly backup and restore each virtual hard disk that is associated with a virtual machine.

Common Property Values for a Windows Azure Virtual Hard Disk

To store the location for all Data Disks, we can use the Get-AzureDataDisk cmdlet. Since VM’s can be provisioned with multiple Data Disks, this cmdlet returns of a collection of DataVirtualHardDisks.

$vmDataDisks = $vm | Get-AzureDataDisk

Deprovision Virtual Machine to Restore

When a virtual machine is provisioned in Windows Azure, the platform places a lease on each virtual hard disk to ensure that disks are not inadvertently deleted.  This means that we’ll need to export the virtual machine configuration and remove the virtual machine first before the restore process can succeed.  Luckily, we can easily accomplish these tasks with just a bit more PowerShell code.

First, we’ll create a local folder in which our exported VM configuration will be stored.

$exportFolder = "C:\ExportVMs"

if (!(Test-Path -Path $exportFolder)) {

            New-Item -Path $exportFolder -ItemType Directory



Next, we’ll export our virtual machine configuration to a local XML file.  We’ll use this exported file at the end of the restore process to reprovision our virtual machine once the virtual hard disk backups have been restored.

$exportPath = $exportFolder + "\" + $vm.Name + ".xml"

$vm | Export-AzureVM -Path $exportPath


Once our virtual machine configuration is safely exported to an XML file, we can now use the Remove-AzureVM cmdlet to remove the virtual machine in preparation of restoring each virtual machine hard disk from the backup blob location.

Remove-AzureVM -ServiceName $vm.ServiceName -Name $vm.Name

Restore Windows Azure VM OS Disk

To restore the relevant OS virtual hard disk from backup, we’ll first need to define a few variables for the virtual hard disk name, blob, container and storage accounts.  We’ll use these variable values during the restore process.

$vmOSDiskName = $vmOSDisk.DiskName

$vmOSDiskuris = $vmOSDisk.MediaLink

$StorageAccountName = $vmOSDiskuris.Host.Split('.')[0]

$vmOSBlobName = $vmOSDiskuris.Segments[-1]

$vmOSOrigContainerName = $vmOSDiskuris.Segments[-2].Split('/')[0]

$backupContainerName = “backups”


After removing a Windows Azure VM, sometimes there’s a short period of time where the virtual disk is still listed as being attached to the virtual machine.  Before continuing, we’ll want to wait until the virtual disk is detached by using the Get-AzureDisk PowerShell cmdlet inside a While loop.

While ( (Get-AzureDisk -DiskName $vmOSDiskName).AttachedTo ) { Start-Sleep 5 }

Once the OS Disk is detached, you’ll be able to remove the disk in preparation for restoring from backup by using the Remove-AzureDisk cmdlet with the –DeleteVHD parameter.

Remove-AzureDisk -DiskName $vmOSDiskName -DeleteVHD

After removing OS disk and associated VHD, you can restore the VHD from backup using the Start-AzureStorageBlobCopy cmdlet and the Get-AzureStorageBlobCopyState cmdlet as follows:

Start-AzureStorageBlobCopy -SrcContainer $backupContainerName -SrcBlob $vmOSBlobName -DestContainer $vmOSOrigContainerName –Force

Get-AzureStorageBlobCopyState -Container $vmOSOrigContainerName -Blob $vmOSBlobName –WaitForComplete

Since we’re performing a blob-to-blob copy up in the Windows Azure cloud platform, the copy operation should complete very quickly.  When the copy has completed, you can add the Azure disk back into your subscription for the restored virtual machine OS disk.

Add-AzureDisk -DiskName $vmOSDiskName -MediaLocation $vmOSDiskuris.AbsoluteUri -OS Windows

This completes the process for restoring the OS disk from a cloud-based backup.

Restore Windows Azure VM Data Disks

In addition to an OS disk, Windows Azure virtual machines may also be attached to one or more persistent data disks.  We can use a similar process for restoring these disks to what we used above for the OS disk.  However, since the data disks are returned as a collection, we’ll run the commands inside a ForEach loop to process each data disk in turn.

ForEach ( $vmDataDisk in $vmDataDisks ) {

        $vmDataDiskName = $vmDataDisk.DiskName

        $vmDataDiskuris = $vmDataDisk.MediaLink

        $vmDataBlobName = $vmDataDiskuris.Segments[-1]

        $vmDataOrigContainerName = $vmDataDiskuris.Segments[-2].Split('/')[0]

        While ( (Get-AzureDisk -DiskName $vmDataDiskName).AttachedTo ) { Start-Sleep 5 }

        Remove-AzureDisk -DiskName $vmDataDiskName –DeleteVHD

        Start-AzureStorageBlobCopy -SrcContainer $backupContainerName -SrcBlob $vmDataBlobName -DestContainer $vmDataOrigContainerName –Force

        Get-AzureStorageBlobCopyState -Container $vmDataOrigContainerName -Blob $vmDataBlobName –WaitForComplete

        Add-AzureDisk -DiskName $vmDataDiskName -MediaLocation $vmDataDiskuris.AbsoluteUri


After running this snippet, each of the data disks that were attached to the virtual machine will be restored.

Reprovision Virtual Machine

Once all of the virtual hard disks have been restored, we can use the Import-AzureVM cmdlet to reprovision the virtual machine.  The reprovisioned VM will automatically point to the restored copy of each virtual hard disk.

Import-AzureVM -Path $exportPath | New-AzureVM -ServiceName $vm.ServiceName

When the import process has completed, the virtual machine will be restored and started in your Windows Azure subscription.

Congratulations! But keep learning!

You’ve completed the process for creating backups and restoring virtual machines in Windows Azure with Windows PowerShell! You can use the cmdlets and snippets in this article series to quite easily build an automated approach to capture a backup of each Windows Azure virtual machine in your subscription, perhaps on a nightly basis.

In addition, you may want to leverage these resources to continue your learning about Windows Azure Infrastructure Services: