Posts Tagged ‘Powershell’

Windows 8 Hyper-V : Powershell script to create vm’s based on csv file

March 3, 2012 Leave a comment

This is my first PowerShell 3.0 script to create Virtual Machine based on .csv file which contains the necessary information.

I created this script when in the Hands on Hyper-V PowerShell session at the MVP Global Summit, in Redmond.

Save the following .csv file ( I saved it on c:\vms )

OperatingSystem,MinimumMemoryMB,RecommendMemoryMB,BaseVhdPath Win7,512,1024,c:\VHD\7601.17514.101119-1850_x86fre_Enterprise_en-us_VL.vhd 2008R2,1024,2048,c:\VHD\WS2008R2_Enterprise_x64.vhd


Here is the Powershell script:


$vms=import-csv C:\vms\OperatingSystems.csv

foreach ($vm in $vms) {

#this command creates the VM with the recommend memory

New-VM -Name $vm.operatingsystem -MemoryStartupBytes $vm.RecommendedMemoryMB

# this command set’s the minimum memory for the OS

Set-Vm -Name $vm.operatingsystem -MemoryMinimumBytes $vm.MinimumMemoryMB

#how cool is the new command to extract the path and join to a new string…

$vhpath= join-path (Split-Path $vm.basevhdpath -Parent) -ChildPath ($vm.operatingsystem+’Diff.vhdx’)

#this command create a new differencing disk and attach it to a vm

New-VHD –ParentPath $vm.BaseVhdPath –Path $vhdpath -Differencing Add-VMHardDiskDrive -VMName $vm.operatingsystem -Path c:\vhd\Diff.vhdx