Change the OS disk used by an Azure VM using PowerShell
If you have an existing VM, but you want to swap the disk for a backup disk or another OS disk, you can use Azure PowerShell to swap the OS disks. You don't have to delete and recreate the VM. You can even use a managed disk in another resource group, as long as it isn't already in use.
The VM does need to be stopped\deallocated, then the resource ID of the managed disk can be replaced with the resource ID of a different managed disk.
Make sure that the VM size and storage type are compatible with the disk you want to attach. For example, if the disk you want to use is in Premium Storage, then the VM needs to be capable of Premium Storage (like a DS-series size). Both disks must also be the same size.
Get a list of disks in a resource group using Get-AzDisk
Get-AzDisk -ResourceGroupName myResourceGroup | Format-Table -Property Name
When you have the name of the disk that you would like to use, set that as the OS disk for the VM. This example stop\deallocates the VM named myVM and assigns the disk named newDisk as the new OS disk.
# Get the VM $vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM # Make sure the VM is stopped\deallocated Stop-AzVM -ResourceGroupName myResourceGroup -Name $vm.Name -Force # Get the new disk that you want to swap in $disk = Get-AzDisk -ResourceGroupName myResourceGroup -Name newDisk # Set the VM configuration to point to the new disk Set-AzVMOSDisk -VM $vm -ManagedDiskId $disk.Id -Name $disk.Name # Update the VM with the new OS disk Update-AzVM -ResourceGroupName myResourceGroup -VM $vm # Start the VM Start-AzVM -Name $vm.Name -ResourceGroupName myResourceGroup
To create a copy of a disk, see Snapshot a disk.