Jan 30 2014

Create multiple VMs using PowerCLI

A couple of days ago i received a request from a customer asking for a PowerCLI script to create VMs. There were a couple of requirements for the script.

  • Specify the number of VMs to be created.
  • The VMs must be powered on after the creation.
  • The VMs must be placed on all ESXi hosts in the vSphere cluster.
  • Specify a vCenter Server Folder in the Virtual Machine & Templates view where the VMs will be placed.
  • Specify VM datastore/datastore cluster placement.
  • Specify the number of VM vCPUs.
  • Specify the number of VM RAM.
  • Specify the VM disk size.
  • Specify the VM disk type (Thin, Thick or Eager Zeroed Thick)
  • Specify the VM guest OS type.
  • Specify the VM prefix
  • Use numeric incremental number after the VM prefix.

Below you’ll find the script i send to my customer that he used for creating and powering on 500 VMs, yes on one single NFS based datastore, and placed them randomly on the ESXi hosts in the vSphere cluster.
I can imagine the next question will be to clone VMs but i’m not sure:)

Updated 2014-02-25 – As i suspected, here you can find a post in which i create multiple VMs, based on one existing vSphere VM, using PowerCLI. 

Change the following parameters in the below script to match your environment:

  • $vCenter=”vc-demo01.vcdx56.com”
  • $vCenterUser=”vcdx56@magnus”
  • $vCenterUserPassword=”notsecret”
  • $vm_count = “500”
  • $numcpu = “2”
  • $MBram = “2048”
  • $MBguestdisk = “4096”
  • $Typeguestdisk =”Thin”
  • $guestOS = “winNetStandardGuest”
  • $ds = “VCDX56-DS01”
  • $Folder = “Scripttest”
  • $Cluster = “VCDX56-02”
  • $VM_prefix = “VCDX56-“

This script was tested using the following software versions:

  • VMware vCenter Server 5.5
  • VMware ESXi 5.5
  • VMware PowerCLI 5.5
  • Microsoft PowerShell 2.0

Thanks to Luc Dekens & Christoffer Zettermark for script input.


3 pings

Skip to comment form

  1. Jeff

    Thanks to share this script. Do you think it’s possible to deploy multiple VMs using an existing VM template ?

  2. magander3

    I’ll publish another blog post this or next week when using a template to do pretty much the same thing. Stay tuned.

  3. kookin

    Thanks for the post! When you used Get-Random for the vm dispersion on the hosts, did it not use the default max value of 32 and generate host totals that were not in range? I am writing a similar script and using the min and max paramenters just to be sure..

  4. magander3

    my vSphere cluster only contained a few ESXi hosts so i didn’t bother about that but it’s a valid point you got there.


  5. Générateur

    An impressive share! I have just forwarded this onto a colleague who had
    been doing a little homework on this. And he actually bought me dinner due to
    the fact that I stumbled upon it for him… lol.
    So let me reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending some time to talk about this issue here on your web page.

  6. magander3

    No problem, hope it was a good dinner:)


  7. madhav

    what should i mention for Linux on Guest OS part ?

  8. magander3

    depends on what Linux Guest OS you will install.


  9. sriram

    nice and thanks for posting.. plz provide in easier steps

  10. Manuel Rabell

    I was wondering what is the fail ratio while using the script? we have try in multiple ocations and 1 out 3 failed, and we had to wait 1 to 2 days to see if it failed on not.

  11. magander3

    for the versions i tested the script i had zero failures. What versions are you running, what do you have to 1-2 days to see if it fails when you can just start the scrip manually and what failure do you get?


  12. Phillip Balderos

    Awesome Script but I am running into this error right away

    PS C:\PowerShellScripts\VMBuildAutomation> C:\PowerShellScripts\VMBuildAutomation\VmBuilder.ps1
    Connecting to vCenter Server vm-vcs.wolfpack.local
    Connect-viserver : The term ‘Connect-viserver’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
    if a path was included, verify that the path is correct and try again.
    At C:\PowerShellScripts\VMBuildAutomation\VmBuilder.ps1:44 char:1
    + Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -War …
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Connect-viserver:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  13. magander3

    do you start the script in a powercli window?


  14. Cyrus Balderos

    The script works in my new 6.0 ESXi enviorment. I don’t think that is what fixed it though. I think I just figured out over time and learning what I was looking at.
    Is there an option to build a vm from a template?

  15. magander3

    take a look at this, http://vcdx56.com/2014/02/25/create-multiple-vms-from-existing-vsphere-vm-using-powercli/ post and see if it provides what you are looking for.


  16. Matt

    How would you go about using this script to deploy multiple VMs from a CSV file? I am attempting to use your code and add the Import-Csv command and encountering some issues.

  17. magander3

    are you looking at importing VM names or why do you need the CSV file?


  18. Nawal Singh

    This script `working fine. I need create 100 VMs shell with the name of TEST33 to TEST100. I need where exactly need to change the value in this script to create vms start with TEST33 to TEST100.

  19. magander3

    haven’t tested this but start with changing line 30 to only TEST, change row 12 to be 67 and then change row 38 so the first number is 33.


  20. Nitesh Kumar


    could you please explain the line 45 ,46 and 47

    -WarningAction 0
    1..$vm_count | foreach {
    $y=”{0:D2}” -f $_


  21. magander3

    it takes care of the potential warning message from the connect-viserver command and the two last lines create the numeric increment to be added to the VM-prefix.


  1. Create multiple VMs using PowerCLI | TUSHAR TOPALE

    […] Create multiple VMs using PowerCLI. […]

  2. Newsletter: February 3, 2014 | Notes from MWhite

    […] And you never know when you might find that handy!  I have several times.  See the article here and be aware he is going to update the article in the near future to support virtual machines from […]

  3. Create multiple VMs from existing vSphere VM using PowerCLI | VCDX56

    […] recently published the blog post “Create multiple VMs using PowerCLI” and as i mentioned in the post i thought the next request would be a PowerCLI script to create […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">