Skip navigation
virtual servers

FAQ: Create an Azure Image from an Existing Virtual Machine

Q: How can I create an Azure image from an existing IaaS virtual machine?

A: You can use PowerShell to capture an existing virtual machine, including its entire disk configuration (i.e., if the machine had additional data disks, those disks would become part of the captured image, including the data on those disks) to a new Azure image. Ideally, the virtual machine being captured should have been SYSPREP'd (generalized) prior to capture (although this isn't required) and should be shut down to ensure that no possible corruption to the new image's state or content can occur (again, this isn't required). Make sure you have the latest PowerShell module for Azure, as explained in "How to install and configure Azure PowerShell." Then use the following PowerShell code:

#Capture an Azure VM to a template

#Recommend to stop first
Stop-AzureVM -ServiceName savtecheastus -Name sandbox

#Save the specified virtual machine to a new image (sandboximage in this example). Because this VM has
#NOT had SYSPREP run I use OSState Specialized but if it had been SYSPREP'd I would use -OSState Generalized
Save-AzureVMImage -ServiceName savtecheastus -Name sandbox -ImageName "sandboximage" -OSState Specialized

#Save a reference to the new image
$sandboxImg = Get-AzureVMImage -ImageName sandboximage

#Set some variables I would use if setting a provisioning configuration
$admin = "localadmin"
$myPwd = "V3ryHardTh!ngT0Gue33"

#Create a new VM config using the new image. Below is assuming the image is not generalized. 
$newVM = New-AzureVMConfig -Name "Sandbox2" -InstanceSize Basic_A1 -ImageName $sandboxImg.ImageName

#If the image had been specialized I would add a provisioning config to command, for example:
#$newVM = New-AzureVMConfig -Name "Sandbox2" -InstanceSize Basic_A1 -ImageName $sandboxImg.ImageName `
#    | Add-AzureProvisioningConfig -Windows -AdminUsername $admin -Password -$myPwd

#Create a new virtual machine based on the above configuration
New-AzureVM -ServiceName savtecheastus -VMs $newVM -WaitForBoot -Verbose

#Remove an image
Get-AzureVMImage -ImageName sandboximage | Remove-AzureVMImage -DeleteVHD

Note that capture is also possible through the Azure portal; the same guidance to shut down the virtual machine applies, although (again) doing so isn't required.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish