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 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.
Generalize the Windows VM using Sysprep
Sysprep removes all your personal account information, among other things, and prepares the machine to be used as an image. For details about Sysprep, see How to Use Sysprep: An Introduction.
Make sure the server roles running on the machine are supported by Sysprep. For more information, see Sysprep Support for Server Roles
If you are running Sysprep before uploading your VHD to Azure for the first time, make sure you have prepared your VM before running Sysprep.
- Sign in to the Windows virtual machine.
- Open the Command Prompt window as an administrator. Change the directory to %windir%\system32\sysprep, and then run
- In the System Preparation Tool dialog box, select Enter System Out-of-Box Experience (OOBE), and make sure that the Generalize check box is selected.
- In Shutdown Options, select Shutdown.
- When Sysprep completes, it shuts down the virtual machine. Do not restart the VM.
You can also generalize a Linux VM using
sudo waagent -deprovision+user and 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
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.