Open In App

Microsoft Azure – Create VM Image from an existing Windows VM

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Azure

In this article, we will show you how to create an image from an existing VM in Azure using the azure PowerShell script. To implement this operation you should have an existing shared image gallery and shared image definition to store the image version captured from an exiting azure windows VM.

Pre-requisites:

  • Owner or Contributor or Custom Read and Write Role to deploy resources within subscription or resource group.
  • Image Gallery and Image Definition Resources.

Steps to Create VM Image from an Existing Windows VM

Step 1: Login to Azure Portal

Step 2: Access Azure Cloud Schell and select PowerShell

Step 3: Create a file named createImage.ps1

$ touch createImage.ps1
touch createImage.ps1

 

Step 4: Open the file createImage.ps1 using the below command to code

code createImage.ps1
code createImage.ps1

 

Copy and Paste the below script in createSharedImgDef.ps1 and modify the variables of your choice

## Get Source VM
$vmImage = Get-AzVM -Name "add vm name"
-ResourceGroupName "add vm resource group"

## Create Image Version
New-AzGalleryImageVersion `
   -GalleryImageDefinitionName "add Gallery DefName" `
   -GalleryImageVersionName "add Gallery Version" `
   -GalleryName "add Gallery Name" `
   -ResourceGroupName "add image galley rg" `
   -Location "add image galley loction" `
   -TargetRegion @{Name='add location';ReplicaCount=1}  `
   -Source $vmImage.Id.ToString() `
   -PublishingProfileEndOfLifeDate "add publisher end of life"

Example:

## Get Source VM
$vmImage = Get-AzVM -Name "WindowsVM" 
-ResourceGroupName "Test-RG"

## Create Image Version
New-AzGalleryImageVersion `
   -GalleryImageDefinitionName "winImage" `
   -GalleryImageVersionName "1.0.0" `
   -GalleryName "winImageGallery" `
   -ResourceGroupName "imgGalleryRG" `
   -Location "eastus" `
   -TargetRegion @{Name='East US';ReplicaCount=1}  `
   -Source $vmImage.Id.ToString() `
   -PublishingProfileEndOfLifeDate "add publisher end of life"
powershell

 

Step 5: Save the file and execute the file using run command

./createImage.ps1
execute

 

Once you are done. Go to Image Gallery >> Image Definition >> Check the Image Version from azure portal.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads