Renaming VHDX Files for Hyper-V VMs

In my lab I try to keep a somewhat consistent naming standard for the virtual hard disks (VHDX Files) I use for my VMs. Every now and then I miss, or I rename a VM later, and there wants to change the name of the VHDX file. In this post you learn how to change the VHDX filename using Hyper-V Manager and PowerShell.

Renaming the VHDX file GUI style

If you only have a single VHDX file to rename, I don't see much harm in changing it via File Explorer and the Hyper-V Manager. However, if you have multiple VMs to update, skip to the PowerShell section.

To change the name of a VHDX file there a few things you need to do:

  1. Ensure the VM is turned off (no saved state, it needs to be turned off)
  2. Ensure there are no Checkpoints
  3. Using File Explorer, rename the VHDX file.
  4. Edit the settings of the VM, and modify the path to the VHDX file
Configuring the path to a VHDX file.

Renaming the VHDX file using PowerShell

Below you find a PowerShell script that will loop through a list of VMs and make sure the VHDX file name is matching the VM name. The script includes some basic validations as well.

# Sample Script to rename the VHDX connected to a VM
# Note #1: This script only deals with a single VHDX per VM
# Note #2: This script uses a simple list of VMs to work with. Can easily be updated to generate a list from wildcard etc. 
#
# Author: Johan Arwidmark
# Twitter: @jarwidmark
# LinkedIn: https://www.linkedin.com/in/jarwidmark
#
# The process is shorthand: 
#  1. Basic validation (no snapshots, not running, no saved state, single VHDX etc.)
#  2. Rename the VHDX file
#  3. Edit the VM VHDX path

# List of VMs to work with
$VMs = @(
    #"TEST06"
    "TEST07"
    #"TEST08"
)

$DiskSuffix = "-Disk0"

# Loop through each VM and make the change
foreach ($VMName in $VMs){
    # VM must exist and be turned off
    $VM = Get-VM $VMName -ErrorAction SilentlyContinue
    If ($VM){
        $VMOff = $VM | Where-Object { $_.State -ne 'Off' }
        If ($VMOff){
            $CurrentState = (Get-VM $VMName).State
            Write-Warning "The $VMName VM must be turned off. Current state is: $CurrentState. Aborting script..."
            break
        }
    }
    Else {
        Write-Warning "VM with $VMName name not found. Aborting script..."
        Break
    }

    # Don't mess with VMs having snapshots
    $Snapshot = Get-VMsnapshot –VMname $VMName
    If ($Snapshot){
        Write-Warning "This VM has one or more snapshots, aborting script..."
        break
    }

    # Get current VHDX file
    $CurrentVHDXFile = Get-VM -VMName $VMName | Get-VMHardDiskDrive
    $VHDXFileCount = ($CurrentVHDXFile | Measure-Object).Count

    If ($VHDXFileCount -eq 1){
        # Single VDHX found, all good. Base new VHDX file name on VM name, and add the suffix
        $NewDiskName = (Get-Item $CurrentVHDXFile.Path).Basename + $DiskSuffix + ".vhdx"
    
        # Renaming the VHDX and store new name in the Results FileInfo object
        $Result = Rename-Item $CurrentVHDXFile.Path -NewName $NewDiskName -PassThru

        # Configure the VM to use renamed VHDX, using existing controller type
        $CurrentControllerType = $CurrentVHDXFile.ControllerType
        $NewVHDXFile = $result.FullName
        Set-VMHardDiskDrive -VMName $VMName -Path $NewVHDXFile -ControllerType $CurrentControllerType
    }
    Else {
        Write-Warning "This VM has either no or multiple VHDX files attached, aborting script..."
    }
}
About the author

Johan Arwidmark

3.5 2 votes
Article Rating
Subscribe
Notify of
guest
5 Comments
Newest
Oldest Most Voted
Greg
Greg
1 year ago

IF you try to rename the folder or the disk file, Windows will throw an error stating that the resource is in use. So you either turn off Hyper-V or remove the virtual machine and then do the renaming. The first option is extremely good choice when you're in production environment so you're left with the second option. Turns out if you remove a virtual machine from your inventory, unlike ESXi, it REMOVES the virtual machine config file from the disk, so that you cannot re-add it later. And to add to the comedy, "remove virtual machine" does not remove… Read more »

Last edited 1 year ago by Greg
Jon
Jon
6 months ago
Reply to  Greg

I'm not sure there was any need to mindlessly bash MS because you don't understand it.

But, Johan is missing a step. You need to remove the VHDX from the VM. Once that's done, you can rename it, and readd it.

There's no need to remove the VM itself from HyperV

Dylan W Wallace
Dylan W Wallace
2 years ago

This very valuable!


>