HL Tools – Part 1 – Clone a VM without vCenter

In my homelab I try to automate as much as possible. I have a number of scripts and functions that help me setting up test environments in my homelab.  Since I got quite a number of requests on this, I decided to start a series on my homelab tools.

One of the tasks I automated is the provisioning of VMs. Quite easy when you have a vCenter that manages the cloning process. But in layer 1 of my homelab I’m running ESXi standalone on a PaaS provided server, so no vCenter.

With the Copy-DatastoreItem cmdlet it is easy to clone the files of a VM, but this kind of copy doesn’t know about the different types of VirtualDisk you can have in a VM. As a result, your Thin vdisks become Thick vDisks in the clone. The function in this post avoids that problem by using the VirtualDiskManager for copying the VMDK.


Note that there are a couple of prerequisites: the master VM needs to be powered off and have no snapshots. And till now I only tested this with VMs that run a Windows guest OS.

The Script


Line 24-30: The definition of the parameters for the function. Note that the function doesn’t use any of the advanced features, nor pipeline input. Since I use this function in my homelab, I found functionality more important than good coding style 😉 And the function name itself doesn’t comply with the approved verbs either.

Line 33-36: The function tests if the master VM is powered on or has snapshots. In that case the function returns immediately. It seems that copying a VM that has snapshots, can have an impact on the functioning of the cloned VM (see KB1000936).

Line 38-40: Get the managers via the ServiceInstance. Remember, no vCenter !

Line 42-46: Retrieve and/or compose the values that are required later in the function.

Line 50: Create the datastore folder that will hold the cloned VM

Line 47,53-54: The function uses the HostDatastoreBrowser to find all VMDK files.

Line 59-80: These lines make sure that the VirtualDisk type of each vDisk in the master VM, is also used in the corresponding vDisk in the cloned VM.

Line 81: The actual copy of the vDisk from the master to the clone.

Line 88: There is no need to copy the .vmdk (already done), the .log and the .vmsd (no snapshots remember) files to the clone.

Line 89: It seems that some PowerCLI builds have a problem when the Item object is passed over the pipeline to the Copy-DatastoreItem cmdlet. To bypass this problem, the function uses a ForEach loop and specifies the Item object on the cmdlet explicitly.

Line 95,101: To update the .vmx and .vmxf files, the function copies both files to a Temp folder on the station where the script is running. This is to bypass the “cannot open file” problem when updating a file in place.

Line 110: The ESXi server will see that the clone has been moved or copied when we try to start the VM. The Set-VMQuestion selects the “copied” answer, which is the default.

Sample Usage


The function is quite easy to use.

Note that the cloned VM will not be customised !

It will have the same configuration as the master VM from which it was cloned. The guest OS customisation, as can be done through the OSCustomizationSpec when there is a vCenter, is the subject for another post in this series.

What I normally do is to prepare my master with everything that I want to have on the cloned VMs. And then I run sysprep with the Shutdown option.


When the cloned VM is started, it will start with the mini-setup.

Enjoy !


    YVS Kumar

    Hi LucD – I’m trying to run this script on esxi 6.0, does this script works on 6.0 , do you have any latest script for 6.0.

    Exception calling “MakeDirectory” with “3” argument(s): “Current license or ESXi version prohibits execution of the requested


      From the error message it looks as if you might be using a so-called “free ESXi” license. This license has some limitations, one of them being that you cannot make changes to the environment with PowerCLI. You can only use PowerCLI in “read mode” I’m afraid.


    scripted this to run from a ESXi box, see


      Although I normally don’t condone running scripts in the ESXi BusyBox, this is a nice port and handy for a lab environment.

    Deepak George

    Do you any possible ways to do a storage vMotion of VM from one datastore to another without using the vCenter.?


    I love the idea of the script, however, when I try and execute it nothing happens. No errors, no warnings, no activity in the VMware console. This is what I’m using to execute:

    .\vmclone.ps1 Clone-MasterVM -MasterName W2K12Core_Base -CloneName Server1 -DatastoreName datastore2 -Register -PowerOn

    Help is greatly appreciated


      Hi William, the script is a function, did you call the function in that .ps1 file ?
      There is a sample call of the function near the end of this post.


    How do I specify the server to perform this procedure in, and the credentials to do so?


      @JDShots, you will need to be connected to an ESXi server.
      You can do that with

      Connect-VIServer -Server esx1.lucd.info -User root -Password password

      Of course you will need to change the hostname and the credentials to reflect your environment.


    Is there a way to download your script?


      Hi JDShots, when you double-click in the code block, all the code will be highlighted. You should then be able to do a copy/paste from the browser to your editor.

      Would you prefer to have all the scripts available as file downloads as well ?


    Hello, Luc! Thanks for the post. I’m trying to write my own version with hot cloning capabilities.
    My question is about the VM config changes between the snapshots are created. Where can I track these changes? vmsd file contains only hard disk drive information. TIA


      Hi, you could use the events as I described in Events – Part 3 : Auditing VM device changes


    Thanks for the script. Is this also a supported method for cloning a VM in a production environment? Thanks!


      Hi Frank, I’m not sure that this method is officially supported by VMware.
      Although it uses public API and does an edit of the VMX file, I’m not sure what the official stance of VMware on this is.
      It would be best to ask for confirmation from VMware Support before you consider using this in a production environment.


    useful script for work in Home!!
    just realize Service Instance is bigger thing thn i thought if all its contents can be use with standalone esxi as it can be use with vCenter; Can it be???



      The ServiceInstance is indeed available on any vSphere server, beit a vCenter or an ESXi server.
      Note that some properties will not be available for all vSphere servers. For example the OptionManager.

    Francois-Xavier Cat

    Awesome script LucD, this is so useful! will give it a try this week


      Thanks, I hope to post some more articles in this series in the coming weeks.

Leave a Reply

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


Buy the Book