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"
)

# 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
    
        # Renaming the VHDX and store new name in the Results FileInfo object
        $Result = Rename-Item $CurrentVHDXFile.Path -NewName "$($VMName).vhdx" -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

5 1 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Dylan W Wallace
Dylan W Wallace
21 days ago

This very valuable!


>