Create Hyper-V VDI/VM by name from CSV file
I was working on a Hyper-V VDI project for a customer recently, who needed to easily deploy a VDI Template VHD from a CSV file and create VMs based on this VHD. I put this script together to fit this requirement.
It references a CSV file called vdi.csv, which contains your desired Hyper-V VM names. It then builds as many copies of the VM as your CSV contains:
Here’s the script!
$VDIs = get-content .\vdi.csv
foreach ($VDI in $VDIs){
write-host $VDI is being copied from a VHD template. This will take approxmiately 2 minutes…
copy “F:\Hyper-V\Virtual Hard Disks\vdi-template.vhd” “F:\Hyper-V\Virtual Hard Disks\$VDI.vhd”
New-VM -Name $VDI -BootDevice IDE -VHDPath “F:\Hyper-V\Virtual Hard Disks\$VDI.vhd” -MemoryStartupBytes 2GB -SwitchName “External”
Set-VMMemory -VMName $VDI -DynamicMemoryEnabled $true -MinimumBytes 512MB -StartupBytes 2GB -MaximumBytes 4GB
Set-VM -Name $VDI -AutomaticStopAction Shutdown
write-host $VDI Has now been created and ready for use…
}
It’s a quick and dirty script – I’ll look at building it out as future requirements dictate.