You dont have javascript enabled! Please enable it!

Rename Azure VM Data Disks

5 Min. Read

In this article, we will share with you how to rename Azure VM data disks with PowerShell.

Introduction

Renaming the OS and Data Disk for Azure virtual machines 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 create or add a wrong data disk name to a Virtual Machine using the Azure Portal, Azure PowerShell, ARM template, Bicep, or Azure CLI. The OS and Data disk name can be specified at creation time using any of your preferred Infrastructure as code (IaC) deployment tools.

If you are following a standard naming convention best practices and you want to rename the Data disk for a virtual machine, then you have to follow the steps below in the Azure Portal:

1) Create a copy of the existing Data Disk with the desired new name (assuming you are using managed disks).
2) Detach the Data disk from the VM.
3) Save and update the VM.
4) Attach the Data disk to the VM and set the right host caching.
5) Finally, save and update the VM.

Rename Azure VM Data Disks
Rename Azure VM Data Disks

The good news with data disks, you don’t need to shut down the VM to attach and detach the disk compared to the OS Disk. However, if you have important data that are stored on the data disk and are frequently accessed by the VM, then it’s recommended to shut down the VM first before you rename the data disk.

Unfortunately, Azure does not have a built-in option to rename the Data Disks. The good news is, that we can automate the entire process using a few lines of PowerShell.

> If you want to rename the OS Disk for an Azure virtual machine, please check the following article.

> If you want to rename the NIC interface for an Azure virtual machine, please check the following article.

> If you want to rename an Azure virtual machine, please check the following article.

In this article, we will share with you how to rename the Data Disk for an Azure virtual machine with PowerShell. The same can apply whether you are using a Linux or Windows VM.

Rename Azure VM Data 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.2.x (core) with the Az module installed.

To install and update the AZ PowerShell module on your machine, run the following command:

# Make sure you have the latest version of PowerShellGet installed 
Install-Module -Name PowerShellGet -Force

# Install and update to the latest Az PowerShell module
Install-Module -Name Az -AllowClobber -Force

.EXAMPLE

.\Rename-AzDataDisk.ps1 -VMName [VMName] -lunDataDisk [1] -newDataDiskName [NewDataDiskName] -Verbose

This example will rename the Data Disk for the specified VM, you need to specify the virtual machine name, the LUN number of the existing data disk, and the new data disk name.
The script will create a copy of the existing data disk and then use the detach/attach disk feature in Azure to change the data disk on the fly without turning off the VM.

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

Rename-AzDataDisk.ps1
Rename-AzDataDisk.ps1

PowerShell Code

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

<#
.SYNOPSIS Rename Azure Data Disks.

.DESCRIPTION Rename Azure VM Data Disks for Linux and Windows.

.NOTES File Name : Rename-AzDataDisk.ps1
Author   : Microsoft MVP/MCT - Charbel Nemnom
Version  : 1.0
Date     : 22-March-2022
Update   : 23-March-2022
Requires : PowerShell 5.1 or PowerShell 7.2.x (Core)
Module   : Az Module
OS       : Windows or Linux VMs

.LINK
To provide feedback or for further assistance please visit: https://charbelnemnom.com

.EXAMPLE
.\Rename-AzDataDisk.ps1 -VMName [VMName] -lunDataDisk [1] -newDataDiskName [NewDataDiskName] -Verbose
This example will rename the Data Disk for the specified VM, you need to specify the VM name, the LUN number of the existing data disk, and the new data disk name.
The script will create a copy of the existing data disk and then use the detach/attach disk feature in Azure to change the data disk on the fly.
#>

[CmdletBinding()]
Param (
    [Parameter(Position = 1, Mandatory = $True, HelpMessage = 'Enter Azure Virtual Machine name')]
    [Alias('VM')]
    [String]$VMName,

    [Parameter(Position = 2, Mandatory = $true, HelpMessage = 'Enter the Logical Unit Number (LUN) of the existing Data Disk that you want to rename')]
    [Alias('DataDiskLun')]
    [String]$lunDataDisk,

    [Parameter(Position = 2, Mandatory = $true, HelpMessage = 'Enter the new Data Disk name')]
    [Alias('DataDiskName')]
    [String]$newDataDiskName

)

#! 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!"
    Exit
}

$azSubs = Get-AzSubscription
$lookVM = 0
foreach ( $azSub in $azSubs ) {
    Set-AzContext -Subscription $azSub | Out-Null

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

    if ($VM) {
        $lookVM++

        #! Get source Data Disk information
        Write-Verbose "Get the source Data Disk information: $diskName"
        $dataDiskInfo = ($VM.StorageProfile.DataDisks | where-object { $_.Lun -eq "$lunDataDisk" })
        if (!$dataDiskInfo) {
            Write-Warning "The Data Disk LUN Number $lunDataDisk cannot be found. Please check the Logical Unit Number. Exiting!"
            Exit
        }
        $sourceDataDisk = Get-AzDisk -ResourceGroupName $vm.resourceGroupName -DiskName $dataDiskInfo.Name

        #! Create the managed disk configuration
        Write-Verbose "Create the managed data disk configuration..."
        $diskConfig = New-AzDiskConfig -SkuName $sourceDataDisk.Sku.Name -Location $VM.Location `
            -DiskSizeGB $sourceDataDisk.diskSizeGB -SourceResourceId $sourceDataDisk.id -CreateOption Copy

        #! Create the new data disk
        Write-Verbose "Create the new Data Disk: $newDataDiskName"
        $newDataDisk = New-AzDisk -Disk $diskConfig -DiskName $newDataDiskName -ResourceGroupName $vm.resourceGroupName

        #! Detach the old data disk
        Write-Verbose "Detach the old data disk: $($dataDiskInfo.Name)"
        Remove-AzVMDataDisk -VM $VM -Name $dataDiskInfo.Name | Out-Null
        #! Updates the state of the Azure virtual machine.
        Write-Verbose "Updates the state of the Azure virtual machine: $VMName"
        Update-AzVM -ResourceGroupName $vm.resourceGroupName -VM $VM

        #! Attach the new data disk
        Write-Verbose "Attach the new data disk: $newDataDiskName"
        if ($dataDiskInfo.caching -like "None") {
            $dataDisk = Add-AzVMDataDisk -VM $vm -Name $newDataDiskName -Lun $lunDataDisk `
                -CreateOption Attach -ManagedDiskId $newDataDisk.Id
        }
        else {
            $dataDisk = Add-AzVMDataDisk -VM $vm -Name $newDataDiskName -Caching $dataDiskInfo.caching -Lun $lunDataDisk `
                -CreateOption Attach -ManagedDiskId $newDataDisk.Id
        }
        #! Updates the state of the Azure virtual machine.
        Write-Verbose "Updates the state of the Azure virtual machine: $VMName"
        Update-AzVM -ResourceGroupName $vm.resourceGroupName -VM $VM

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

If ($lookVM -eq 0) {
    Write-Warning "The Azure VM < $VMName > cannot be found. Please check your virtual machine name!"
}

The PowerShell tool will look into all your subscriptions to find the virtual machine if you have more than one subscription, and then it will check if the data disk exists before it starts with the renaming process.

If you look back in the Azure Portal, we can see that the new Data Disk name is created and attached successfully.

New data disk name attached
New data disk name attached

Summary

In this article, we showed you how to rename the Data Disk for an Azure virtual machine with PowerShell whether you are using a Linux or Windows VM.

> If you want to rename the OS Disk for an Azure virtual machine, please check the following article.

> If you want to rename the NIC interface for an Azure virtual machine, please check the following article.

> If you want to rename an Azure virtual machine, please check the following article.

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. Happy Renaming of Azure Data Disks!

__
Thank you for reading my blog.

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

-Charbel Nemnom-

Photo of author
About the Author
Charbel Nemnom
Charbel Nemnom is a Senior Cloud Architect, Swiss Certified ICT Security Expert, Certified Cloud Security Professional (CCSP), Certified Information Security Manager (CISM), Microsoft Most Valuable Professional (MVP), and Microsoft Certified Trainer (MCT). He has over 20 years of broad IT experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems with extensive practical knowledge of complex systems build, network design, business continuity, and cloud security.
Previous

Celebrate World Backup Day 2022 and WIN with @AltaroSoftware

Install System Center Virtual Machine Manager 2022 on Windows Server 2022 and SQL Server 2019

Next

2 thoughts on “Rename Azure VM Data Disks”

Leave a comment...

Let me know what you think, or ask a question...

error: Alert: The content of this website is copyrighted from being plagiarized! You can copy from the 'Code Blocks' in 'Black' by selecting the Code. Thank You!