How to batch create virtual machines in Windows Server 2012(PowerShell)


This PowerShell script illustrates how to batch create multiple virtual machines based on comma delimited file by using PowerShell 3.0 in Windows Server 2012.

Download Windows Server 2012
Download Your FREE Copy of Hyper-V Server 2012



Get Microsoft Silverlight


IT admin requires to batch creating virtual machines in Windows Server 2012, although they can use few commands due to the lack of programming knowledge. Although it’s a set of Hyper-V command-lets within Windows PowerShell, IT Admins are reluctant to use them except simple a command which is widely used.


Step1: First, we need to prepare the comma delimited CSV file. It contains valuable list of information about the virtual machines. The detailed information is given in the following table:








Specifies the path of virtual hard disk folder.

Specifies the name of the new virtual hard disk. (such as “Windows Server 2012.vhdx” or “Windows Server 2012.vhd”)

Specifies the path to the parent of the differencing disk to be created

Specifies the path of virtual machine folder.

Specifies the name of the new virtual machine.

Specifies the amount of memory, in Megabytes, to assign to the virtual machine.

Specifies the name of the virtual switch if you want to connect the new virtual machine to an existing virtual switch to provide connectivity to a network. (Noteļ¼šif we do not have any virtual switch to configure, we can leave this parameter blank.)

Step 2: Run the script in the Windows PowerShell Console, type the command: Import-Module <Script Path> at the prompt.

For example, type Import-Module E:\BatchCreateVirtualMachines.psm1

This is shown in the following figure:

Step 3: We can type the command Get-Help New-OSCSVirtualMachine to display the entire help file for this function, such as the syntax, parameters, or examples.

Step 4:  To create the virtual machines run the command New-OSCVirtualMachine  –CsvFilePath <path name>

Finally, when the script completes, it will return a report in the current Windows PowerShell Console. The report contains details of the virtual machines created.

Now, in Hyper-V Manager, you will see the virtual machines have been created.

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

if($isExisted -eq $null)  
    Write-Warning "Can not find virtual switch : $VMSwitchName, because the virtual switch does not exist." 
    #creates a virtual hard disk  
    New-VHD -Path $VHDPath -ParentPath $VHDParentPath -Differencing|Out-Null 
    #creates a virtual machine. 
    New-VM -Path $VMFolder -Name $VMName -VHDPath $VHDPath -MemoryStartupBytes $(Invoke-Expression -Command $MemoryStartupBytes)|Out-Null 
    #connects a virtual network adapter to a virtual switch 
    Connect-VMNetworkAdapter -VMName $VMName -SwitchName $VMSwitchName 



Windows Server 2012

Windows PowerShell 3.0

Hyper-V 3.0


Additional Resources

Hyper-V Cmdlets