Virtualization, Cloud, Infrastructure and all that stuff in-between

My ramblings on the stuff that holds it all together

Quick and Dirty PowerShell to create a large number of test VMs with sequential names


Be gentle, I’m new to this PowerShell stuff – I have a requirement to create a large number of VMs from a template, this is the PowerShell Code I hacked together from a VMTN communities blog post – it’s not pretty but it works for me – you can play with the variables to adjust to your own environment and desired number of VMs.

In my case my template is a Linux VM setup ready to boot from a LiveCD – just so it generates some basic load when it starts up.

There is a bit of clever number formatting which I lifted from this blog post to pad the VM numbers out to 3 digits and make it tidy looking, not entirely sure I understand what it does – but it works!

I am using PowerGUI based on the info at Al’s blog here

Connect-VIServer -Server localhost >$null

$NameVM ="vmNested-"
$NameTemplate ="TPL – vmNested-01"

$Date=get-date -uformat "%Y%m%d"

$NumArray = (1..$HOW_MANY_TO_CREATE)

foreach ($number in $numArray )
$name =  $seqn | % {"{0:0##}"          -f $_}
$string = $NameVM + $name
echo Creating $string
New-VM -template (Get-template $NameTemplate) -Name $string -Datastore (Get-datastore $Datastore) -VMHost $ESX


The Results (40 VM’s from template – completed in about 5mins);

image image


7 responses to “Quick and Dirty PowerShell to create a large number of test VMs with sequential names

  1. Pingback: VMware vSphere - виртуализация ЦОД » Как создать сразу несколько тестовых виртуальных машин из шаблона на VMware vSphere / ESX.

  2. Thomas Lee March 16, 2010 at 11:49 pm

    Nice Script.

    I have a couple of suggestions that would make the code a tad more elegant, as follows:

    $NameVM =”vmNested-”
    $NameTemplate =”TPL – vmNested-01″

    $Date=get-date -uformat “%Y%m%d”

    $NumArray = (1..$HOW_MANY_TO_CREATE)

    for ([int]$seq=1;$seq -le $HOW_MANY_TO_CREATE;$seq++)
    $string = $NameVM + ($seq.tostring(“0##”))
    “Creating $string”
    # Create the VM
    New-VM -template (Get-template $NameTemplate) -Name $string -Datastore (Get-datastore $Datastore) -VMHost $ESX

    • vinf March 17, 2010 at 11:57 am

      Thanks Thomas, the examples you have on your site are probably the most useful I have found… thanks!

  3. Pingback: Top Virtualization Blog Voting Time « Virtualization, Windows, Infrastructure and all that stuff in-between

  4. maqsood June 15, 2011 at 9:08 am

    but where do i need to run this script,

    i have PC with 8Gb memory but due to h/w incompaitability i am unable to install esx on thats, so can i run ESXi on Vmware wks(on win7) and then create mutiple ESi host inside Main ESXi(which will be running on vmware wks)

  5. June 16, 2011 at 12:41 pm

    @Maqsood – that would work from a script point of view allowing you to create the VMs but you won’t be able to start any of the vmESX nodes running inside another vmESXi running on workstation as you can’t have a 64-bit nested VM (VM inside another VM)

    You could have an ESXi and a vCenter VM on your physical machine then use this script then talks to the vCenter (which controls the vmESXi) and create a bunch of x86 VMs that would run inside the vmESXi VM – that would work, just doesn’t work with x64 VMs.


  6. Gurjit Dhillon October 27, 2011 at 4:49 pm


    First of thanks for wonderfull script, I am able to deploy the multiple vm by this script but also also getting some error. I am not sure why these error are coming, is it possible to get some infomation ?

    New-VM : 10/27/2011 5:40:48 PM New-VM Operation is not valid due to the current state of the object.
    At C:\Users\gudhillo\AppData\Local\Temp\1d2ba134-a7e1-440b-9d45-c1e30bbc396e.ps1:32 char:7
    + New-VM <<<< -Name $string -Template $(get-template $strTemplate) -location (get-folder $strLocation) -pool (get-resourcepool $strPool) -VMHost $(Get-VMHos
    t $strDestinationHost) -Datastore $(Get-Datastore $strDatastore) -OSCustomizationSpec $(Get-OSCustomizationSpec $strCustomSpec)
    + CategoryInfo : NotSpecified: (:) [New-VM], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

    Gurjit Dhillon

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: