Share via


Microsoft Azure: How to deploy a Virtual Machine from existing OS disk using PowerShell

In this article, we will learn how we can use the existing OS disk to deploy a new virtual machine.

As we know that VHD files are stored in storage account. We will follow the same process of creating a new VM that we have learnt from previous few blogs but in this process we need to specify the disk name while creating virtual machine.

To get the disk name we can use the cmdlet Get-AzureDisk and copy the DiskName value from that.

You can use below script to deploy a VM from existing OS disk:

#define all the values to variables needed to create virtual machine
 
$location = "South India"
 
$cloudservicename = "kkazurevm03"
 
$vmsize = "Basic_A2"
 
$vmname = "kkazurevm03"
 
$username = "kkazure"
 
$password = "Kk@12345"
 
$OSdiskname = "kkazurevm02-kkazurevm02-0-201708221319170311"
 
#get the subscription name from login account
 
$subscriptionname = (Get-AzureSubscription).SubscriptionName
 
Set-AzureSubscription -SubscriptionName $subscriptionname -CurrentStorageAccountName "kkazurestore02"
 
#Assigning configuration
 
$config = New-AzureVMConfig -Name $vmName -InstanceSize $vmsize -DiskName $OSdiskname
 
#Create new Azure VM
 
New-AzureVM -ServiceName $cloudServiceName -Location $location -VMs $config

Once the script gets executed we will see the succeeded status.

We can also check the information of newly created VM from management portal.

So friends, this is how we can deploy a new virtual machine using the existing OS disk which can help out when we need multiple VMs with same OS configuration.