As someone who writes about a wide variety of technical subjects, I find myself creating a lot of virtual machines. Since I don’t want to have to create these virtual machines manually, I tend to use the virtual machine templates feature in System Center Virtual Machine Manager (VMM) to automate the virtual machine creation process. If you’ve ever set up templates within VMM however, then you know that the process can sometimes be a bit complicated.
Over time I have found that there are some situations in which VM templates are really necessary. First, VM templates are a must if you have a Hyper-V-based private cloud or hybrid cloud, and need self-service virtual machine provisioning capabilities. Second, VM templates can aid in the deployment of application servers.
Sometimes though, what you really need is just a fast way of deploying a Windows VM. In those situations, going through the trouble of creating templates may be overkill (or may not be an option if you don't have VMM). Microsoft makes it easy to set up a model VM, and then create clones of the VM using Hyper-V Manager, PowerShell, or VMM. So in this article, I want to show you an easy way to create and clone simple, generic Windows VMs, without the hassle of creating a VM template or a traditional deployment image.
Preparing the model virtual machine
The first step is to create a virtual machine that you can clone. Remember that any clones that you create will be nearly identical copies of the model VM, so be sure to provision your model VM with the appropriate hardware allocations.
After creating the VM, install Windows in the same way that you normally would, but with a couple of caveats. You won’t need to worry about using a product key, and you shouldn’t join the model VM to a domain. Once the installation finishes, be sure to install any updates that may be available.
The next step in the process is to run Sysprep. Sysprep is a utility that is included with the Windows operating system. Sysprep gets rid of machine specific operating system settings such as the computer name. This process is known as generalizing the operating system.
To execute Sysprep, log onto the VM and go to C:\Windows\System32\Sysprep, and run Sysprep.exe, as shown in the figure below.
As you can see in the above image, the Sysprep interface is really simple. Although there are only three configuration options, these options must be set correctly for this process to work as intended. The System Cleanup Action needs to be set to Enter System Out-of-Box Experience (OOBE). The Generalize checkbox must be selected, and the Shutdown Options section must be set to Shutdown.
Once you have selected the correct options, click OK and Sysprep will get to work, as shown in the figure below. Once Sysprep finishes preparing Windows, it will shut down the operating system.
At this point, you have created a VM that is ready for cloning. Before you do anything else, however, I recommend check-pointing your model VM. That way, you have an easy way of returning to a pristine, sysprepped state in the event that any changes are accidentally made to the VM.
Cloning the VM
Now that you have created a model that is suitable for cloning, you can create clones using Hyper-V Manager, VMM, or even PowerShell. I will show you all three techniques.
Cloning the model VM from Hyper-V Manager
If you want to clone your model VM using Hyper-V Manager, right click on the model VM, and choose the Export command shown in the image below. When you do, the Hyper-V Manager will ask you where you want to save the export files to. Now, just enter an export path, and click OK.
Once the export completes, the next step is to import the VM. To do so, right-click on your host server, and select the Import Virtual Machine command from the shortcut menu. This will launch the Import Virtual Machine Wizard.
Click Next to bypass the wizard’s Welcome screen, and you will be taken to a screen asking you to specify the folder containing the VM that you want to import. Choose the location of your exported VM, and click Next. Keep in mind that the VM is stored in a subfolder beneath the export location and you will have to specify this subfolder.
Select the VM that you want to import, and click Next. Finally, when you get to the Choose Import Type screen, choose the Copy the Virtual Machine option, and click Next. Now, just follow the prompts to specify a storage location and then click Finish.
When the process completes, the Hyper-V Manager will display two identical VMs, both bearing the name of your model VM. Rename one of the VMs, and delete its checkpoint. You can now start the newly created VM. Upon doing so, the VM will launch the Windows minisetup wizard, and will be ready to use a short time later.
Cloning the model VM from VMM
If you are using VMM, then you can clone your model VM by going to the VMs and Services workspace, right-clicking on the VM, and choosing the Create | Clone commands from the shortcut menu. This will cause VMM to launch the Create Virtual Machine Wizard. This is the same wizard that you use when you create a VM from scratch.
The nice thing about this wizard is that it gives you a chance to name the new VM so you don’t have to worry about renaming it later on. You can accept the defaults on the wizard’s remaining screens unless you have a compelling reason to make a change.
Cloning the model VM from PowerShell
Assuming that you do not have VMM, cloning a VM through PowerShell involves using the Export-VM and Import-VM cmdlets. The Export-VM cmdlet requires you to provide the name of the VM that you want to export, and the export path. Such a command might look like this:
Export-VM -Name MyVM -Path F:\VMs
Because the import process needs to register the VM in place, the syntax used for the Import-VM cmdlet is really simple. You need only to provide the import path. Such a command might look like:
Import-VM -Path ‘F:\VMs\MyVM\VirtualMachines\59B9AA53-ABDB-B23F-C6772020383b.XML’
The XML file referenced in the command above is created by the export process, and the filename is unique to each VM. You can see an example of such a file in the image below.
If you need help with the import and export process, you can find Microsoft’s documentation. When you are done, you can rename the VM by using the Rename-VM cmdlet.
Perfect for generic deployments
This method of cloning is great for generic Windows deployments. However, if you need a bit more control over the process (such as the ability to join a domain), then you can create a Sysprep answer file to be used by Windows Setup.
Photo credit: Shutterstock