How To Swap OS Disk In Azure VM

Swapping the OS disk of an Azure VM is very useful for certain scenarios, like troubleshooting issues or upgrading the operating system. In this article, we will walk you through all those approaches.

How To Swap OS Disk In Azure Virtual Machine

Let us dive deep into these two approaches individually.

  • Approach -1: Using Azure Portal
  • Approach – 2: Using Azure PowerShell

Approach -1: Using Azure Portal

Follow the below steps

1. Log in to Azure Portal (https://portal.azure.com/).

2. Search for Virtual Machine and click on the search result in Virtual Machines as shown below.

azure swap os disk

3. Now, you will see the lists of Azure Virtual machines that you have created. Click on the one for which we will swap the OS disk.

azure vm swap os disk

4. On the Virtual Machine page, click the Disks option present under Settings from the left navigation, then click on the swap OS disk option as highlighted below.

how to swap os disk in azure vm

5. On the swap OS disk window, select the new managed disk from the Choose disk dropdown. If no other managed disk is available, it will show “no managed disks available.” Then confirm you want to swap the OS disk for this VM by entering the exact name of the VM, as shown in the screenshot below. Finally, click on the OK button, as shown below, and you are done.

azure swap os disk no managed disks available

Now, you can able to see the disk has been changed successfully. Check out the screenshot below.

how to change os disk in azure vm

Approach – 2: Using Azure PowerShell

You can also use the Azure PowerShell script below to swap the OS disk in your Azure VM using PowerShell.

# Get your Virtual Machine details 
$myvm = Get-AzVM -ResourceGroupName newresgroup -Name AzureLessonsVM 

# Stop your VM
Stop-AzVM -ResourceGroupName newresgroup -Name $myvm.Name -Force

# Retrieve the new disk details
$newdisk = Get-AzDisk -ResourceGroupName NEWRESGROUP -Name AzurelessonsVM2_disk1_56b6a3d6fa3c46d09882b933432ea6e5

# Configure your VM to point to the new os disk  
Set-AzVMOSDisk -VM $myvm -ManagedDiskId $newdisk.Id -Name $newdisk.Name 

# Update your virtual machine with the new disk
Update-AzVM -ResourceGroupName newresgroup -VM $myvm 

After executing the above PowerShell script, I got the expected output as below.

How to swap os disk in Azure VM PowerShell

Now, to cross-check, I logged in to the Azure Portal and saw that the new OS disk had been swapped successfully. Check out the screenshot below for your reference.

Swap the OS disk used by an Azure VM using PowerShell

Video Tutorial

You may also like following the articles below.

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!