It is not uncommon in consulting to work out of dedicated VMs for each customer. This helps create a strong segregation between internal and each client workstream. If you use Hyper-V, chances are that you already have a template VM and use copies of it, so you don’t setup everything over and over. In this post, I will do exactly that, but introduce as much automation using PowerShell as possible.
IMPORTANT: All commands shown from now on are to be executed in PowerShell in the host computer as Admin.
1. Installing Hyper-V
The following command will install Hyper-V along with the PowerShell module for Hyper-V management. You may need to restart your computer before you can proceed to next steps.
2. Creating the template VM
We have 2 options here. You can either DYI or you can use MS starting VM template.
The advantage of DYI is customization. You can use Windows 10 or 11 or even older versions. Also, your copy of windows will be very clean. The disadvantage is, you have to do it all yourself and there is no easily automated way to get through Windows installation and initial setup.
Before starting, you will need a windows installation file as an
.iso. You can download that from MSDN if you have a license.
This script will create the VM, its disk, and bind the iso as a DVD drive. When started, the VM will automatically start windows installation.
Once the VM starts, finish windows installation, create a local user, and you should be ready to continue preparation in step 3.
2.2 MS provided VM
MS already has a win 11 VM (as of August 2022) that you can just download at developer.microsoft.com. This will be a zip with a
.vhdx file. You still need to create a VM and attach the disk to it:
NOTE: Before you run the powershell below, unzip the
vhdxdownloaded and move it to
The caveat of this model is that the VM will have some pre-installed software. For instance, the VM has Visual Studio Community edition which is not applicable to my typical environment.
3. License your template VM
Whichever approach you use for creating your base VM, eventually, it will need to be activated. If you have an MSDN, you can get a product key from my.visualstudio.com and activate windows with it.
IMPORTANT Not all licenses can be used in multiple VM copies. Be careful with how you utilize your license.
4. Standard DEV tools
My previous post on Automated and repeatable Environment Setup - winget has good instructions on how to setup your environment using winget which should be provided in win 11 by default.
5. Export VM
IMPORTANT: before going further, ensure that everything you need is installed in the template VM.
The Export-VM will basically create a copy of your template that can be easily imported with Import-VM later. If you don’t export your VM, you will not be able to import it.
You may delete your original template VM if you want to save on space. You can always create a new one from the
.vmcx file and make changes to it before exporting again.
6. Import the VM as a copy
Importing your VM as a copy will create a new id for it. This way you will be able to import the same
.vmcx many times. Also, you can pipe the import result and rename the VM easily.
Bonus tip 1 - full screen resolution
You may have to enable Enhanced Session in the VM itself.
Bonus tip 2 - leave full-screen
Just like VIM, I never remember how to get out of a full-screen session where the remote desktop bar is not available. There are a few ways to get out of this situation:
- CTRL + ALT + Pause/Break
- CTRL + ALT + Left Arrow
- This will only release the mouse pointer from the VM, press windows key to open the start menu on the host computer and then you can minimize or close the remote desktop session.
You may automate the creation of VMs easily. However, whenever possible, I recommend using Ephemeral environments. After initial configuration, you should easily be able to spin up and destroy those environments very easily and they should be reasonably affordable. I have discussed briefly about Ephemeral Environments earlier this month in Getting started with Ephemeral environments.