Move Multiple Hyper-V Storage with Powershell script

Move Multiple Hyper-V Storage with Powershell script

July 16, 2020

If you have the need to move multiple VM disks on a stand-alone Hyper-V server or Hyper-V cluster server, this script may help you move them. If you perform manually VM disk migration for multiple VM, you will need to finish the one VM first before you can proceed to the next VM. Hence, this can prevent you from waiting as the script will handle it and move to the next VM disks.

This is basically a loop script that will move the VM storage from the old location to the new location.

On this scenario. I will migrate the VMs storage from old CSV (cluster shared storage) volume to the new volume
Old path -> C:\ClusterStorage\NesCluDisk1\Hyper-V
New Path -> C:\ClusterStorage\NesCluDisk2\Hyper-V

I assume that the volume or the LUN is ready to use, and already connected to the all clusters nodes or host.

First, format the iSCSI drive via from the one of servers. Format it as NTFS filesystem, and 64K allocation unit size.

Add the disk to the cluster, and then add into Cluster Shared Storage

Once done, there will be two volumes on C:\ClusterStorage. To easy to manage the volume, rename the volume with a easy name like volume2,3 etc. In my environment, I rename it as NesCluDisk2

Once the cluster shared volume is ready to use, check the existing VM, and its storage path to ensure the VM name, and its directory structure. Create the same directory if necessary. On my environment, I want to move them to the same Hyper-V directory on the new cluster storage, hence I created a new Hyper-V directory on the new volume.

Use this command to show the current VM disk path.

Get-VM -ComputerName (Get-Cluster) | Get-VMHardDiskDrive | select VMName,Path

Use this script to move the VMs to the new CSV volume. Change any parameter to match your environment.

## New directory path on the CSV ##
$StoragePath = "C:\ClusterStorage\NesCluDisk2\Hyper-V\"

#List of VMs needs to be moved on the cluster. Remove (get-clusternode) if want to run on the stand-alone server ##
$VMs = Get-VM -ComputerName (Get-ClusterNode) 

## Looping the task ##
Foreach ($VM in $VMs) {
$VMStorage = $StoragePath
Write-Host "Moving VM:" $VM.name "to" $VMStorage
Move-VMStorage -VMName $VM.name -DestinationStoragePath $VMStorage
}

When script is running, it will move the VM one by one.

Wait until the task complete. Once done, please recheck the VM storage path with this command, to ensure all VMs is successfully moved to the new volume.

Get-VM -ComputerName (Get-Cluster) | Get-VMHardDiskDrive | select VMName,Path  

As you can see, all of my VMs have been successfully moved to the new CSV storage.

Thank you, and hope this helps.

Leave a Comments

Your email address will not be published. Required fields are marked *

Copyright ©2023 All rights reserved