Clone an Azure virtual machine using Snapshots and PowerShell

In this post I will share the method I used to clone a development Azure Virtual Machine.  My task was to replicate a development SharePoint environment. We wanted to make sure it stayed as is, so using sysprep and templates didn’t fit the requirements.  I started searching and came across the following two articles that I used to put together a script to create the clone and required azure resources from a snapshot.

The script gives you a replica of the virtual machine from a snapshot stored in Azure. There are considerations to keep in mind, the vm needs to be created in a new resource group and VNet to avoid conflicts with the existing original vm.


  • Take a snapshot of the VM you want to clone from the Azure Portal.
  • Gather the following information to be enter into the script as it runs
    • Azure Subscription ID – obtain from Azure Portal
    • Resource Name Prefix – this text will be used as the prefix for all resources created during the clone process
    • Location – Azure location to create the clone
    • Resource Group Snapshot exists in – name of the resource group that contains the snapshot you want to clone
    • Snapshot Name – the name of the snapshot to create the new VM from.
    • Size of the virtual machine – the name of the size of VM to be created, if you need to generate a list to view options use the following powershell
      Get-AzureRmVMSize -Location canadaeast

Create VM from Snapshot script

#Login to Azure
#Provide Subscription Id
$subscriptionId = Read-Host -Prompt "Subscription Id"
Select-AzureRMSubscription -SubscriptionId $subscriptionId
#Prompt for resource name prefix
$name = Read-Host -Prompt "Resource name prefix"
#Prompt for location to create resources in
$location = Read-Host -Prompt "Location"
#Prompt for resource group snapshot exists in
$SourceRGname = Read-Host -Prompt "Resource Group Snapshot exists in"
#Prompt for snapshot name to create VM from
$snapshotName = Read-Host -Prompt "Snapshot Name"
#Provide the size of the virtual machine
#e.g. Standard_D4_v3
#Get all the vm sizes in a region using below script:
#e.g. Get-AzureRmVMSize -Location canadaeast
$virtualMachineSize = Read-Host -Prompt "Provide the size of the virtual machine Ex. Standard_D4_v3"
#Virtual Machine Name
$virtualMachineName = $name + "VM"
#Generate name of the OS disk that will be created using the snapshot
$rnd = Get-Random
$osDiskName = $name.ToLower() + "disk" + $rnd
#Generate name of the Resource Group that new vm will be created in
$rgName = $name + "-RG"
Write-Host "Creating Resource Group..."
New-AzureRmResourceGroup -Name $rgName -Location $location
#Generate name of the Virtual Net that new vm will use
$subnetName = $name + "Net"
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix
$vnetName = $name + "Vnet"
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix -Subnet $singleSubnet
#Set the context to the subscription Id where Managed Disk will be created
Select-AzureRmSubscription -SubscriptionId $SubscriptionId
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $SourceRGname -SnapshotName $snapshotName 
$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $rgName -DiskName $osDiskName
#Initialize virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
#Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows
#Create a public IP for the VM 
$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $rgName -Location $snapshot.Location -AllocationMethod Dynamic
#Get the virtual network where virtual machine will be hosted
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
# Create NIC in the first subnet of the virtual network 
$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $rgName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id
#Create the virtual machine with Managed Disk
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $rgName -Location $snapshot.Location

Running the script

  • Run the script in PowerShell
  • You will be prompted to login to your Azure subscription, complete the process
  • Answer the prompts with the information gathered from the prerequisites section.
  • Wait for the script to complete, you now have a new Resource Group containing the replica VM for you to use.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

Up ↑