How To Rename The OS Disk For An Azure Virtual Machine

4 min read


Renaming the OS Disk for an Azure VM is not a new problem. If you find yourself having to do this, you know how painful it is. This is the case when you deploy a Virtual Machine using the Azure Portal, however, if you deploy the VM using Azure PowerShell, ARM template, or Azure CLI, then you can specify the OS disk name at deployment time.

Once the VM is deployed using the portal, Azure will end up creating a random name for the OS disk as showing in the following screenshot:

If you are following a standard naming convention and you want to rename the OS disk, then you have to follow the steps below in the Azure Portal:

  1. Create a copy of the OS Disk with the desired name (assuming you are using managed disks).
  2. Swap the OS disk for the VM.
  3. Start the VM.
  4. finally, delete the old OS disk.

You don’t need to shut down the VM first, but once you Swap the OS disk, then you need to start the VM through the portal. Unfortunately, Azure does not have a built-in option to rename the OS disk. The good news is, we can automate the entire process using a few lines of PowerShell and skip the start VM step.

In this blog post, I will share with you how to rename the OS Disk for an Azure VM with PowerShell. The same can apply whether you are using a Linux or Windows VM.

Rename Azure VM OS Disk

You have a couple of options to run the script, you can either use Azure Cloud Shell, Visual Studio Code, or Windows Terminal. The Script works with PowerShell 5.1 or PowerShell 7 (core) with Az module.


.\Rename-AzOSDisk.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -osdiskName [OSDiskName] -Verbose

This example will rename the OS Disk for the specified VM, you need to specify the Resource Group name, VM name, and the new OS disk name. The script will use the Swap OS disk feature in Azure and change the OS disk on the fly. Finally, the script will ask you to confirm the deletion of the original OS disk. If the VM is a member of an Availability Set, the script will also work flawlessly.

Please note that the VM will reboot once the disk is swapped. You need to plan a small maintenance window if it’s an important VM.

Here is an example of the output once you run this tool:

PowerShell Code

The complete script is detailed below to automate the entire process:

.SYNOPSIS Rename Azure OS Disk.

.DESCRIPTION Rename Azure VM OS Disk for Linux and Windows.

.NOTES File Name : Rename-AzOSDisk.ps1
Author   : Microsoft MVP - Charbel Nemnom
Version  : 1.0
Date     : 13-September-2019
Update   : 22-September-2019
Requires : PowerShel 5.1 or PowerShell 7 (Core)
Module   : Az Module
OS       : Windows or Linux VMs

To provide feedback or for further assistance please visit: 

.\Rename-AzOSDisk.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -osdiskName [OSDiskName] -Verbose
This example will rename the OS Disk for the specified VM, you need to specify the Resource Group name, VM name and the new OS disk name.
Then the script will use the Swap OS disk feature in Azure and change the OS disk on the fly.

Param (
    [Parameter(Position = 0, Mandatory = $true, HelpMessage = 'Enter the Resource Group of the VM')]

    [Parameter(Position = 1, Mandatory = $True, HelpMessage = 'Enter Azure VM name')]

    [Parameter(Position = 2, Mandatory = $true, HelpMessage = 'Enter the desired OS Disk name')]


#! Check Azure Connection
Try {
    Write-Verbose "Connecting to Azure Cloud..."
    Connect-AzAccount -ErrorAction Stop | Out-Null
Catch {
    Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!"

#! Get the details of the VM
Write-Verbose "Get the VM information details: $VMName"
$VM = Get-AzVM -Name $VMName -ResourceGroupName $resourceGroup

#! Get source OS Disk information
Write-Verbose "Get the source OS Disk information: $($VM.StorageProfile.OsDisk.Name)"
$sourceOSDisk = Get-AzDisk -ResourceGroupName $resourceGroup -DiskName $VM.StorageProfile.OsDisk.Name

#! Create the managed disk configuration
Write-Verbose "Create the managed disk configuration..."
$diskConfig = New-AzDiskConfig -SkuName $sourceOSDisk.Sku.Name -Location $VM.Location `
    -DiskSizeGB $vm.StorageProfile.OsDisk.DiskSizeGB -SourceResourceId $sourceOSDisk.Id -CreateOption Copy

#! Create the new disk
Write-Verbose "Create the new OS disk..."
$newOSDisk = New-AzDisk -Disk $diskConfig -DiskName $osdiskName -ResourceGroupName $resourceGroup

#! Swap the OS Disk
Write-Verbose "Swap the OS disk to: $osdiskName"
Set-AzVMOSDisk -VM $VM -ManagedDiskId $newOSDisk.Id -Name $osdiskName | Out-Null
Write-Verbose "The VM is rebooting..."
Update-AzVM -ResourceGroupName $resourceGroup -VM $VM

#! Delete the old OS Disk
$delete = Read-Host "Do you want to delete the original OS Disk [y/n]"
If ($delete -eq "y" -or $delete -eq "Y") {
    Write-Warning "Deleting the old OS Disk: $($sourceOSDisk.Name)"
    Remove-AzDisk -ResourceGroupName $resourceGroup -DiskName $sourceOSDisk.Name -Force -Confirm:$false

If you look back in the Azure Portal, we can see that the new OS Disk name is attached successfully.
If you have any feedback or changes that everyone should receive, please feel free to leave a comment below.

That’s it there you have it. Enjoy Azure OS Swapping!

Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

About Charbel Nemnom 512 Articles
Charbel Nemnom is a Cloud Architect, ICT Security Expert and Microsoft Most Valuable Professional (MVP), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems. Excellent communicator is adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design, virtualization, and cloud security.

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.