Create a managed disk from a snapshot with PowerShell
This script creates a managed disk from a snapshot. Use it to restore a virtual machine from snapshots of OS and data disks. Create OS and data managed disks from respective snapshots and then create a new virtual machine by attaching managed disks. You can also restore data disks of an existing VM by attaching data disks created from snapshots.
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell lets you use either
PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.
To launch Azure Cloud Shell:
|Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.|
|Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser.|
|Select the Cloud Shell button on the top-right menu bar in the Azure portal.|
To run the code in this article in Azure Cloud Shell:
- Open Cloud Shell.
- Select the Copy button on a code block to copy the code.
- Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
- Press Enter to run the code.
This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.
#Provide the subscription Id $subscriptionId = 'yourSubscriptionId' #Provide the name of your resource group $resourceGroupName ='yourResourceGroupName' #Provide the name of the snapshot that will be used to create Managed Disks $snapshotName = 'yourSnapshotName' #Provide the name of the Managed Disk $diskName = 'yourManagedDiskName' #Provide the size of the disks in GB. It should be greater than the VHD file size. $diskSize = '128' #Provide the storage type for Managed Disk. PremiumLRS or StandardLRS. $storageType = 'PremiumLRS' #Provide the Azure region (e.g. westus) where Managed Disks will be located. #This location should be same as the snapshot location #Get all the Azure location using command below: #Get-AzLocation $location = 'westus' #Set the context to the subscription Id where Managed Disk will be created Select-AzSubscription -SubscriptionId $SubscriptionId $snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName Â $diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id Â New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName Â
This script uses following commands to create a managed disk from a snapshot. Each command in the table links to command specific documentation.
|Get-AzSnapshot||Gets snapshot properties.|
|New-AzDiskConfig||Creates disk configuration that is used for disk creation. It includes the resource Id of the parent snapshot, location that is same as the location of parent snapshot and the storage type.|
|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 Linux VM documentation.