Share via


Bulk changing virtual hard disk path

I received this in email today:

"I have XCOPY’d a bunch of VHDX files from one volume to another on WS2016.    What’s the easiest / fastest way to fix up the paths for the VM’s???”

The answer to this is quite simple.  Open PowerShell and run:

$oldPath = "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks" $newPath = "D:" get-vm | Get-VMHardDiskDrive | ? path -Like $oldPath* | %{Set-VMHardDiskDrive -VMHardDiskDrive $_ -Path $_.path.Replace($oldPath, $newPath)}

A couple of details on this answer:

  1. PowerShell is wonderful for these kinds of bulk operations
  2. While we do not allow you to edit the virtual hard disk path on a saved virtual machine using Hyper-V manager – we do allow you to do this through PowerShell.  In fact – there are a lot of things that are blocked in Hyper-V manager that are possible through PowerShell.

Cheers,
Ben

Comments

  • Anonymous
    January 26, 2017
    And if you are a noob to PowerShell (we all were once):The '%' is a shorthand alias of ForEach-Object as is "foreach"And the $_ refers to the object being passed in to the script block between the brackets.Which in the example above is the VMHardDiskDrive path parameter.Get- returns the entire VMHardDiskDrive object and the -like selects the parameter that matches the name pattern "$path*" and this value is piped to the script block and referenced using the $_
    • Anonymous
      January 26, 2017
      Thanks for the clarifications! :-)
  • Anonymous
    April 10, 2017
    This won't work when you have VMReplication enabled on the VM(s)...Is there some way through Powershell to workaround that aswell, I hope ?Need to do this on a couple of huge VMs, and removing and re-seeding the Replica would a huge pain...
    • Anonymous
      April 22, 2017
      Have you tried VM Storage Migration ? It should work while VM Replication is enabled.