How To Move VHD Image From One Azure Subscription to Another

2 Min. Read

In this post, I will outline how you can copy a custom VHD image from one Azure subscription to another Azure subscription.

For more information on how to Prepare a Windows VHD or VHDX to upload to Azure, please check the following article.

The scenario is the following:

You have two azure subscriptions, and in one them, you already have Windows Server custom VHD image that you use to create new virtual machines. The goal is, you want to copy this image to the other Azure subscription so you can use it there to create new VMs in that subscription.

How To Move VHD Image From One Azure Subscription to Another 2

What you could do is, copy the VHD image to another storage account of the second subscription. Once the VHD is copied to the second subscription you then can create an new image using that VHD and use it to create VMs. However, this process is lengthy and you will pay extra storage cost to have the same image in two different subscriptions.

Well there is a better approach, you can move resources from one Azure subscription to another, by selecting “Change” next to the Subscription, and then move the resources as shown in the following screenshot. If you want to create new VMs in the first subscription, then you can move it back and forth.

How To Move VHD Image From One Azure Subscription to Another 3

The move is very fast between subscriptions, it will take couple of minutes to move 50GB fixed VHD, and then you are ready to create new VMs in the second subscription.

To take this step even further, I have created the following PowerShell script that will automate the entire process and create a new customized VM in Azure.

Enjoy :)

#! Variables for reuse
$location = 'West Europe'
$sourceResourceGroup = 'source-rg-01'
$destinationResourceGroup = 'destination-rg-01'
$network = "vnet-core-rg-01"
$storagediag = "weuprodlogsto01"

#! Get Blob custom VHD image
$azStorage = Get-AzStorageAccount -ResourceGroupName $sourceResourceGroup
$storageContainer = Get-AzStorageContainer -Context $azStorage.Context
$azblob = Get-AzStorageBlob -Container $storageContainer.Name -Context $azStorage.Context

#! Create the new OS disk from the uploaded VHD
$sourceUri = "$($azblob.ICloudBlob.Container.Uri.AbsoluteUri)/$($"
$osDiskName = 'OSDisk'
$osDisk = New-AzDisk -DiskName $osDiskName -Disk `
(New-AzDiskConfig -AccountType Premium_LRS   `
        -Location $location -CreateOption Import `
        -SourceUri $sourceUri) `
    -ResourceGroupName $destinationResourceGroup

#! Create VM resources
#! Create vNIC
$nicName = "vNic1"
$vnet = Get-AzVirtualNetwork -ResourceGroupName $network
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $destinationResourceGroup
$nic = New-AzNetworkInterface -Name $nicName `
    -ResourceGroupName $destinationResourceGroup `
    -Location $location -SubnetId $vnet.Subnets[0].Id `
    -NetworkSecurityGroupId $nsg.Id

#! Set the VM name and size
$vmName = "NewAzureVM"
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_D4s_v3"

#! Add the vNIC
$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

$vm = Set-AzVMBootDiagnostics -VM $vm -Enable -StorageAccountName $storagediag -ResourceGroupName $destinationResourceGroup

#! Add the uploaded OS disk
$vm = Set-AzVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType Premium_LRS `
    -DiskSizeInGB 50 -CreateOption Attach -Windows

#! Add a managed Data disk
$dataDiskName = 'DataDisk1'
$diskConfig = New-AzDiskConfig -SkuName Premium_LRS -Location $location -CreateOption Empty -DiskSizeGB 100
$dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $destinationResourceGroup
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 0

#! Create the New VM
New-AzVM -ResourceGroupName $destinationResourceGroup -Location $location -VM $vm

Hope this helps!

Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-


Azure Security Center: How to Protect Your Datacenter with Next Generation Security

How To Assess and Migrate Hyper-V Virtual Machines Using Azure Migrate Service


Let me know what you think, or ask a question...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!