Practical App

Create Hyper-V Virtual Machines with Windows PowerShell

If you need to create and configure VMs on a regular basis, using Windows PowerShell to do so can speed up the process.

Whether you need to test new or standardized server setups, Hyper-V gives you an environment to quickly and efficiently run test configurations, which has made it a popular lab environment. The only catch is, you need to create and configure VMs on a regular basis.

If you're looking for an easy way to build Hyper-V images, Windows PowerShell scripts are a viable solution. The extent to which you can customize Windows PowerShell and its flexible command structure makes it suitable for any environment. When combined with an automated or unattended setup strategy, virtually any computer configuration is possible.

Configure Windows PowerShell
This strategy assumes you're working on a Windows Server 2008 R2 system that meets the requirements for the Hyper-V role. After following these steps, the system will be ready for Hyper-V scripting:

Install the Hyper-V role and Windows PowerShell. You can do this from the Server Manager or by using the ServerManagerCmd.exe command-line tool (ServerManagerCmd.exe –install Hyper-V and ServerManagerCmd.exe –install PowerShell).

Configure Windows PowerShell to allow the use of scripts. This is disabled by default. You can configure this with the Set-ExecutionPolicy cmdlet (Set-ExecutionPolicyRemoteSigned).

Download and install the Windows PowerShell Hyper-V module. You need this module to access all the cmdlets designed specifically for Windows PowerShell. From the CodePlex PowerShell Management Library for Hyper-V page (, download the latest stable, non-development release. Make sure the "block" attribute is removed from the zipped files before you install them. This will execute setup scripts without needing digital signatures. If you're doing the install on Server Core, unblock and unzip the module file on another computer before copying the setup files to the server. Otherwise, you'll need additional tools to perform these operations on the server (the stream.exe tool on SysinternalsSuite and 7-Zip).

From an elevated Windows PowerShell command prompt, enable the Hyper-V cmdlets by importing the newly installed module (Import-Module HyperV). If you get an error message, verify that you didn't miss a previous step. If you intend to regularly use Windows PowerShell to manage your Hyper-V environment, you should add the Import-Module and Set-ExecutionPolicy cmdlets to a Windows PowerShell profile file. This will let you use the cmdlets without having to configure the server each time. You should always use an elevated Windows PowerShell command environment to avoid any issues with running the cmdlets.

To verify you've successfully installed the cmdlets and they're functioning properly, use the Get-Command cmdlet to see a list of the commands available to you (Get-Command –Module Hyperv). As with other modules, each cmdlet comes with helpful information about its functions and examples on how to use it (Get-Help New-VM –Detailed and Get-Help New-VM –Examples). There's additional documentation available on the CodePlex PowerShell Management Library for Hyper-V page.

Configure the Hyper-V Windows PowerShell Script
Once your Windows PowerShell environment is ready, you can start building new VMs. Make sure you start with Administrator permissions to use elevated privileges for these commands. The script uses Hyper-V cmdlets to create a new VM based on five variables (see Figure 1) you provide during setup. Each variable has a pre-assigned default value that will be used if one isn't provided.

After defining those variables, the New-Image.ps1 script (see Figure 2) configures the Hyper-V Virtual Network using the value assigned to the $Network1 variable. Before defining the new private network with the New-VMPrivateSwitchcmdlet, remove it with Remove-VMSwitch, whether or not it already existed. This ensures you don't define duplicate networks with the same name.

The final part of the process where you actually create the VM is simple. Create the directory location for the virtual hard drive (VHD). Then six cmdlets create and configure the new VM.

The New-VM command defines the machine and its location. New-VHD creates the VHD file and Add-VMDisk assigns it to the VM. Add-VMDrive adds a DVD drive to the machine, Set-VMMemory defines the amount of RAM and Add-VMNIC configures the network card.

Once you've configured the new VM, you can install whichever OS you'll be using. You can also script and manage the OS installation process with Windows PowerShell. Using Windows PowerShell to automate these tasks can expedite and streamline the entire process, especially if you have to do this on a regular basis.

About the Author

Neil R. Tucker, MCT, MCITP, MCDBA, MCSE, MCDST, has more than 17 years of experience as a technician, trainer and technical writer. He consults and teaches classes on SQL Server, Windows Server, Windows 7 and Windows PowerShell. He is also the author of the Microsoft Learning 50331 Course for Windows 7 Desktop Support Technicians.


comments powered by Disqus

Subscribe on YouTube