Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to relocate Hyper-V VM files to another location

·482 words·3 mins· 100 views · 5 likes ·
Microsoft Microsoft Windows Move-VMStorage Storage

Either because your storage device begins to run out of space, or you simply need to reorganize the files of your virtual machines it is often necessary to move the storage of our virtual machines to a new location. In this post, I want to show you how you can use the Move-VMStorage cmdlet to perform Hyper-V storage migration.

move-vmstorage

Set the variables>

Set the variables #

Here, we define the name of the VM and the hyper-v hostname.

$vmName = "VM01"
$hostName = "HyperVHost"
Move all the files of the VM>

Move all the files of the VM #

To move all the files of the virtual machine (smart paging file, snapshot files, VHDs, and configuration files) to the same location. Use the -DestinationStoragePath parameter. First, define the path where the files will be stored and then run the following PowerShell command as administrator.

$storagePath = "L:\Hyper-V"

Move-VMStorage `
        -ComputerName $hostName `
        -DestinationStoragePath $storagePath `
        -Name $vmName
Move only the configuration files of the VM>

Move only the configuration files of the VM #

if you want to move only the configuration files of the virtual machine to another location. Use the -VirtualMachinePath parameter. Define the path where the files will be stored and then run the following PowerShell command as administrator.

$vmPath = "H:\Hyper-V"

Move-VMStorage `
        -ComputerName $hostName `
        -Name $vmName `
        -VirtualMachinePath $vmPath
Move VHD/VHDX files>

Move VHD/VHDX files #

To move a single virtual disk (VHD / VHDX). You must use the -vhds parameter. This parameter expects to receive a hash table that has two entries. The first entry specifies the current location of the virtual hard disk to move, and has a SourceFilePath key:

"SourceFilePath" = "\..\virtualdisk.vhdx"

The second entry specifies the new location for the virtual hard disk and has a DestinationFilePath key:

"DestinationFilePath" = "\..\virtualdisk.vhdx"

Important: The virtual hard disk name must be identical in both entries. You must declare the hashtable as shown below and then use the Move-VMStorage cmdlet with the following syntax:

$vhd= @{"SourceFilePath" = "L:\Hyper-V\Virtual Hard Disks\virtualDisk.vhdx";
        "DestinationFilePath" = "H:\Hyper-V\Virtual Hard Disks\virtualDisk.vhdx"
       }

Move-VMStorage `
        -ComputerName $hostName `
        -Name $vmName `
        -Vhds $vhd

if you want to move several virtual disks and want to move them to different locations. You must define one or more hash tables within an array as shown below.

$vhds = @(@{"SourceFilePath" = "L:\Hyper-V\Virtual Hard Disks\virtualDisk01.vhdx";
          "DestinationFilePath" = "H:\Hyper-V\Virtual Hard Disks\virtualDisk01.vhdx"
           },
          @{"SourceFilePath" = "L:\Hyper-V\Virtual Hard Disks\virtualDisk02.vhdx";
          "DestinationFilePath" = "\\SmbServer\\Hyper-V\Virtual Hard Disks\virtualDisk02.vhdx"})
One last example>

One last example #

In the following example, I want to show you how to combine the use of various parameters, you can move the vhdx files and the VM configuration files to different locations.

Move-VMStorage `
        -ComputerName $hostName `
        -Name $vmName `
        -VirtualMachinePath $vmPath `
        -Vhds $vhds

Thanks for reading my post. I hope you find it useful.

If you want to know more about the Move-VMStorage cmdlet, check out this link.