Add data disks to multiple VMs from PowerShell

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:\ClusterStorage\VMs\' + $VMname + '\' + $VMname + 'Data1.vhdx'
    $DataDisk2 = 'C:\ClusterStorage\VMs\' + $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


Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.