Copy managed disks to same or different subscription with CLI
This script copies a managed disk to same or different subscription but in the same region. The copy works only when the subscriptions are part of the same Azure AD tenant.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Launch Azure Cloud Shell
The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.
To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.
When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.
Sign in to Azure
Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing
<Subscription ID> with your Azure Subscription ID. If you don't have an Azure subscription, create an Azure free account before you begin.
subscription="<subscriptionId>" # add subscription here az account set -s $subscription # ...or use 'az login'
Run the script
#Provide the subscription Id of the subscription where managed disk exists sourceSubscriptionId="<subscriptionId>" #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 az account set --subscription $sourceSubscriptionId #Get the managed disk Id managedDiskId=$(az disk show --name $managedDiskName --resource-group $sourceResourceGroupName --query [id] -o tsv) #If managedDiskId is blank then it means that managed disk does not exist. echo 'source managed disk Id is: ' $managedDiskId #Provide the subscription Id of the subscription where managed disk will be copied to targetSubscriptionId=6492b1f7-f219-446b-b509-314e17e1efb0 #Name of the resource group where managed disk will be copied to targetResourceGroupName=mytargetResourceGroupName #Set the context to the subscription Id where managed disk will be copied to az account set --subscription $targetSubscriptionId #Copy managed disk to different subscription using managed disk Id az disk create --resource-group $targetResourceGroupName --name $managedDiskName --source $managedDiskId
Clean up resources
Run the following command to remove the resource group, VM, and all related resources.
az group delete --name mySourceResourceGroupName
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.
|az disk show||Gets all the properties of a managed disk using the name and resource group properties of the managed disk. The
|az disk create||Copies a managed disk by creating a new managed disk in different subscription using the
For more information on the Azure CLI, see Azure CLI documentation.
Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.