Add data disks to multiple VMs from PowerShell

Learn how to add multiple VHDs to VMs using PowerShell.

John Savill

September 3, 2015

1 Min Read
Add data disks to multiple VMs from PowerShell

Q. How can I easily add data disks to multiple Hyper-V VMs using PowerShell?

A. I have a test lab and needed to quickly add 2 data disks to 4 VMs I have running 2016 TP3 to set up Storage Spaces Direct. The PowerShell below simply adds two data disks to all the VM names specified in the first array. Note you can also change the path to where you wish to create the disks, by default it creates them in a sub-folder of the VM name under a CSV called VMs.

$VMnames = 'win216tp301','win216tp302','win216tp303','win216tp304'foreach($VMname in $VMnames){    Write-Output "Adding disks to $VMname"    $DataDisk1 = 'C:ClusterStorageVMs' + $VMname + '' + $VMname + 'Data1.vhdx'    $DataDisk2 = 'C:ClusterStorageVMs' + $VMname + '' + $VMname + 'Data2.vhdx'    New-VHD -Path $DataDisk1 -Dynamic -SizeBytes 10GB    New-VHD -Path $DataDisk2 -Dynamic -SizeBytes 10GB    Add-VMHardDiskDrive -VMName $VMname -Path $DataDisk1     Add-VMHardDiskDrive -VMName $VMname -Path $DataDisk2}

 

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like