Tutorial: Create a custom image of an Azure VM with Azure PowerShell

Custom images are like marketplace images, but you create them yourself. Custom images can be used to bootstrap configurations such as preloading applications, application configurations, and other OS configurations. In this tutorial, you create your own custom image of an Azure virtual machine. You learn how to:

  • Sysprep and generalize VMs
  • Create a custom image
  • Create a VM from a custom image
  • List all the images in your subscription
  • Delete an image

Before you begin

The steps below detail how to take an existing VM and turn it into a re-usable custom image that you can use to create new VM instances.

To complete the example in this tutorial, you must have an existing virtual machine. If needed, this script sample can create one for you. When working through the tutorial, replace the resource group and VM names where needed.

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account. Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. There are a few ways to launch the Cloud Shell:

Click Try It in the upper right corner of a code block. Cloud Shell in this article
Open Cloud Shell in your browser. https://shell.azure.com/powershell
Click the Cloud Shell button on the menu in the upper right of the Azure portal. Cloud Shell in the portal

If you choose to install and use the PowerShell locally, this tutorial requires the AzureRM module version 5.7.0 or later. Run Get-Module -ListAvailable AzureRM to find the version. If you need to upgrade, see Install Azure PowerShell module.

Prepare VM

To create an image of a virtual machine, you need to prepare the VM by generalizing the VM, deallocating, and then marking the source VM as generalized in Azure.

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.

  1. Connect to the virtual machine.
  2. Open the Command Prompt window as an administrator. Change the directory to %windir%\system32\sysprep, and then run sysprep.exe.
  3. 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.
  4. In Shutdown Options, select Shutdown and then click OK.
  5. When Sysprep completes, it shuts down the virtual machine. Do not restart the VM.

Deallocate and mark the VM as generalized

To create an image, the VM needs to be deallocated and marked as generalized in Azure.

Deallocated the VM using Stop-AzureRmVM.

Stop-AzureRmVM -ResourceGroupName myResourceGroup -Name myVM -Force

Set the status of the virtual machine to -Generalized using Set-AzureRmVm.

Set-AzureRmVM -ResourceGroupName myResourceGroup -Name myVM -Generalized

Create the image

Now you can create an image of the VM by using New-AzureRmImageConfig and New-AzureRmImage. The following example creates an image named myImage from a VM named myVM.

Get the virtual machine.

$vm = Get-AzureRmVM -Name myVM -ResourceGroupName myResourceGroup

Create the image configuration.

$image = New-AzureRmImageConfig -Location EastUS -SourceVirtualMachineId $vm.ID 

Create the image.

New-AzureRmImage -Image $image -ImageName myImage -ResourceGroupName myResourceGroup

Create VMs from the image

Now that you have an image, you can create one or more new VMs from the image. Creating a VM from a custom image is similar to creating a VM using a Marketplace image. When you use a Marketplace image, you have to provide the information about the image, image provider, offer, SKU, and version. Using the simplified parameter set for the New-AzureRMVM cmdlet, you just need to provide the name of the custom image as long as it is in the same resource group.

This example creates a VM named myVMfromImage from the myImage, in the myResourceGroup.

New-AzureRmVm `
    -ResourceGroupName "myResourceGroup" `
    -Name "myVMfromImage" `
    -ImageName "myImage" `
    -Location "East US" `
    -VirtualNetworkName "myImageVnet" `
    -SubnetName "myImageSubnet" `
    -SecurityGroupName "myImageNSG" `
    -PublicIpAddressName "myImagePIP" `
    -OpenPorts 3389

Image management

Here are some examples of common managed image tasks and how to complete them using PowerShell.

List all images by name.

$images = Get-AzureRMResource -ResourceType Microsoft.Compute/images 
$images.name

Delete an image. This example deletes the image named myOldImage from the myResourceGroup.

Remove-AzureRmImage `
    -ImageName myOldImage `
    -ResourceGroupName myResourceGroup

Next steps

In this tutorial, you created a custom VM image. You learned how to:

  • Sysprep and generalize VMs
  • Create a custom image
  • Create a VM from a custom image
  • List all the images in your subscription
  • Delete an image

Advance to the next tutorial to learn about how highly available virtual machines.