Copy managed disks in the same subscription or different subscription with PowerShell
This script creates a copy of an existing managed disk in the same subscription or different subscription. The new disk is created in the same region as the parent managed disk.
If needed, install the Azure PowerShell module using the instructions found in the Azure PowerShell guide, and then run
Connect-AzureRmAccount to create a connection with Azure. Also, you need to have an SSH public key named
id_rsa.pub in the .ssh directory of your user profile.
This article has been updated to use the new Azure PowerShell Az module. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For installation instructions, see Install Azure PowerShell.
#Provide the subscription Id of the subscription where managed disk exists $sourceSubscriptionId='yourSourceSubscriptionId' #Provide the name of your resource group where managed disk exists $sourceResourceGroupName='mySourceResourceGroupName' #Provide the name of the managed disk $managedDiskName='myDiskName' #Set the context to the subscription Id where Managed Disk exists Select-AzSubscription -SubscriptionId $sourceSubscriptionId #Get the source managed disk $managedDisk= Get-AzDisk -ResourceGroupName $sourceResourceGroupName -DiskName $managedDiskName #Provide the subscription Id of the subscription where managed disk will be copied to #If managed disk is copied to the same subscription then you can skip this step $targetSubscriptionId='yourTargetSubscriptionId' #Name of the resource group where snapshot will be copied to $targetResourceGroupName='myTargetResourceGroupName' #Set the context to the subscription Id where managed disk will be copied to #If snapshot is copied to the same subscription then you can skip this step Select-AzSubscription -SubscriptionId $targetSubscriptionId $diskConfig = New-AzDiskConfig -SourceResourceId $managedDisk.Id -Location $managedDisk.Location -CreateOption Copy #Create a new managed disk in the target subscription and resource group New-AzDisk -Disk $diskConfig -DiskName $managedDiskName -ResourceGroupName $targetResourceGroupName
This script uses following commands to create a new managed disk in the target subscription using the Id of the source managed disk. Each command in the table links to command specific documentation.
|New-AzDiskConfig||Creates disk configuration that is used for disk creation. It includes the resource Id of the parent disk and location that is same as the location of parent disk.|
|New-AzDisk||Creates a disk using disk configuration, disk name, and resource group name passed as parameters.|
For more information on the Azure PowerShell module, see Azure PowerShell documentation.
Additional virtual machine PowerShell script samples can be found in the Azure Windows VM documentation.
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.