Renaming an Azure Windows VM (Managed Disks)


In Azure, the renaming of resources (such as a VM) isn’t allowed.  That is, you can rename the OS/FQDN name of the VM at any time, but the display name in Azure is locked in at creation time.  If this bugs you, vote up the feature request here: https://feedback.azure.com/forums/216843-virtual-machines/suggestions/6132055-rename-vm

In the meantime, by tweaking a Microsoft provided script we can easily rename a VM by deleting the VM object (keeping all disks, NICs, IPs, etc.) and then recreating the VM using those existing objects.  The whole process should take ~10min (although it will vary based on the number of disks and NICs attached).

The below PowerShell script will work as-is for Windows VMs using managed disks and can easily be tweaked to run with Linux VMs or those using storage accounts.


# Nicole Welch, 10 January 2019

# Rename existing Windows VM in Azure Portal (resource name)

# Based on
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/change-availability-set


Add-AzureRmAccount


# Set variables
     $resourceGroup = "Demo"
     $oldvmName = "myVM"
     $newvmName = "newVM"


# Get the details of the VM to be renamed
     $originalVM = Get-AzureRmVM `
        -ResourceGroupName $resourceGroup `
        -Name $oldvmName


# Remove the original VM
     Remove-AzureRmVM -ResourceGroupName $resourceGroup -Name $oldvmName   


# Create the basic configuration for the replacement VM
     $newVM = New-AzureRmVMConfig -VMName $newvmName -VMSize $originalVM.HardwareProfile.VmSize


    Set-AzureRmVMOSDisk -VM $newVM -CreateOption Attach -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id -Name $originalVM.StorageProfile.OsDisk.Name -Windows


# Add Data Disks
     foreach ($disk in $originalVM.StorageProfile.DataDisks) {
     Add-AzureRmVMDataDisk -VM $newVM `
        -Name $disk.Name `
        -ManagedDiskId $disk.ManagedDisk.Id `
        -Caching $disk.Caching `
        -Lun $disk.Lun `
        -DiskSizeInGB $disk.DiskSizeGB `
        -CreateOption Attach
     }


# Add NIC(s)
     foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {
         Add-AzureRmVMNetworkInterface `
            -VM $newVM `
            -Id $nic.Id
     }


# Recreate the VM
     New-AzureRmVM `
        -ResourceGroupName $resourceGroup `
        -Location $originalVM.Location `
        -VM $newVM `
        -DisableBginfoExtension


Comments (3)
  1. Vivek061191 says:

    This is one the old option we are using to rename the VM, delete the VM and using os disk create new VM with New name. Is there any option available like AWS to rename the VM without deleting it?

    1. Nicole Welch says:

      As far as I know (and per the feedback link), this is still the only option.

  2. Hammernaut says:

    Hi Nicole, thanks for the script. I tried this out when helping a customer and it works great. Stripping it of availability set info works as well.
    The customer I was helping was needing to grab Tag information as well. I recommended the following:
    BEFORE REMOVING THE ORIGINAL VM:
    #Get the tags from the original vm and assign them to the variable $tags
    $tags = (Get-AzureRmResource -ResourceGroupName $resourceGroup -ResourceType Microsoft.Compute/virtualMachines -Name $oldvmName).Tags​

    AFTER CREATING THE NEW VM:
    #Assign the $tags to a new VM​
    Set-AzureRmResource -ResourceGroupName $resourceGroup -Name $newvmName -ResourceType “Microsoft.Compute/VirtualMachines” -Tag $tags

    Maybe someone else can benefit from that addition.

Comments are closed.

Skip to main content