This article shows you how to use Azure PowerShell to create an image of a generalized Azure VM. You can then use the image to create another VM. The image includes the OS disk and the data disks that are attached to the virtual machine. The image doesn't include the virtual network resources, so you need to set up those resources when you create the new VM.
- You need to have already generalized the VM. Generalizing a VM removes all your personal account information, among other things, and prepares the machine to be used as an image. You can also generalize a Linux VM using
sudo waagent -deprovision+userand then use PowerShell to capture the VM. For information about using the CLI to capture a VM, see How to generalize and capture a Linux virtual machine using the Azure CLI
- You need to have Azure PowerShell version 1.0.x or newer installed. If you haven't already installed PowerShell, read How to install and configure Azure PowerShell for installation steps.
Log in to Azure PowerShell
Open Azure PowerShell and sign in to your Azure account.
A pop-up window opens for you to enter your Azure account credentials.
Get the subscription IDs for your available subscriptions.
Set the correct subscription using the subscription ID.
Select-AzureRmSubscription -SubscriptionId "<subscriptionID>"
Deallocate the VM and set the state to generalized
Deallocate the VM resources.
Stop-AzureRmVM -ResourceGroupName <resourceGroup> -Name <vmName>
The Status for the VM in the Azure portal changes from Stopped to Stopped (deallocated).
Set the status of the virtual machine to Generalized.
Set-AzureRmVm -ResourceGroupName <resourceGroup> -Name <vmName> -Generalized
Check the status of the VM. The OSState/generalized section for the VM should have the DisplayStatus set to VM generalized.
$vm = Get-AzureRmVM -ResourceGroupName <resourceGroup> -Name <vmName> -Status $vm.Statuses
Create the image
Copy the virtual machine image to the destination storage container using this command. The image is created in the same storage account as the original virtual machine. The
-Pathparameter saves a copy of the JSON template locally. The
-DestinationContainerNameparameter is the name of the container that you want to hold your images. If the container doesn't exist, it is created for you.
Save-AzureRmVMImage -ResourceGroupName <resourceGroupName> -Name <vmName> ` -DestinationContainerName <destinationContainerName> -VHDNamePrefix <templateNamePrefix> ` -Path <C:\local\Filepath\Filename.json>
You can get the URL of your image from the JSON file template. Go to the resources > storageProfile > osDisk > image > uri section for the complete path of your image. The URL of the image looks like:
You can also verify the URI in the portal. The image is copied to a container named system in your storage account.
- Now you can create a VM from the image.