How to Create a Hyper-V VM using PowerShell
Brandon Lee, posted on March 27, 2017
One of the powerful built-in features of Hyper-V is being able to utilize PowerShell for its management operations. Among the many things that we can do with PowerShell in Hyper-V is create virtual machines. The possibilities for automation and quick provisioning are endless. Let’s take a look at a few examples of how to create VMs with PowerShell in Hyper-V.
Running PowerShell in Hyper-V
There are a couple of ways we can run PowerShell in Hyper-V. An easy way to gain access to PowerShell is to remote directly into the Hyper-V server and launch it from there. To remote into a Hyper-V host, we need to make sure Remote Desktop is enabled. Once Remote Desktop is enabled, we can use the Remote Desktop utility and remote into the server as we would any Windows Server GUI.
To check remote desktop settings, you launch the sconfig utility if not already launched and configure using option 7. Also, once remoted into the Hyper-V host, you can use option 15 to Exit to Command Line.
Once there, you are in a regular cmd.exe environment – not PowerShell. To launch a PowerShell environment, simply type powershell at the command line. As you can see, once you are in PowerShell, you can use Hyper-V specific commandlets such as get-vm which shows all the VMs provisioned on the Hyper-V host.
Also, with PowerShell we can remotely interact with Hyper-V. In other words, we can remotely execute commands against our Hyper-V host without establishing a Remote Desktop connection.
From an administrative workstation that we have Hyper-V management tools loaded, we can execute our Hyper-V commandlets. First, we need to install the Hyper-V tools including these commandlets.
- Install-WindowsFeature RSAT-Hyper-V-Tools
Once we have the tools loaded we can run the same types of commands using the -computername parameter pointed to our Hyper-V host like the following. We pass the name of our host to the get-vm commandlet.
Creating a Virtual Machine using PowerShell
To create a new Generation 2 VM with a brand new 60 GB VHDX hard disk and connect it to a specific virtual switch (in this case “ExternalSwitch”) you would run the following.
- new-vm -Name “WSTEST” -MemoryStartupBytes 2GB -Generation 2 -NewVHDPath “D:\hyper-v\virtualhard disks\WSTEST.vhdx” -NewVHDSizeBytes 60000000000 -Switchname “ExternalSwitch”
To add a DVD drive along with an ISO image to boot from, we can run the addition commands:
- Add-vmscsicontroller -vmname WSTEST
- Add-vmdvddrive -vmnname WSTEST -controllernumber 1 -controllerlocation 0 -path D:\ISO\my.iso
If we want to create a fixed size disk and attach it to our VM, we can run the following to create a fixed size VHDX and attach it to our VM.
- new-vhd -path “d:\hyper-v\virtual hard disks.wstest.vhdx” -sizebytes 60GB -fixed
- add-vmharddiskdrive -vmname WSTEST -path “d:\hyper-v\virtual hard disks\wstest.vhdx”
Other Useful Hyper-V PowerShell Commandlets
There is a myriad of very powerful PowerShell commandlets that can control a Hyper-V environment. Check out the Microsoft Hyper-V commandlet reference here: https://technet.microsoft.com/itpro/powershell/windows/hyper-v/index
Just a sampling of the commandlets available:
- get-vm – this displays any VMs provisioned on a Hyper-V host
- get¬-vmhost – displays information about a Hyper-V host
- get-vmmemory – Gets the memory of a virtual machine or snapshot
- get-vmnetworkadapter – Gets the virtual network adapters of a virtual machine, snapshot, management operating system, or of a virtual machine and management operating system.
- get-vmswitch – Gets virtual switches from one or more virtual Hyper-V hosts.
Managing Hyper-V using PowerShell is extremely powerful and provides an easy way to make mass automated changes to our Hyper-V environment. Many of the actions we can perform are much easier than pointing and clicking through the GUI counterparts in the Hyper-V manager. PowerShell can be run locally on the Hyper-V host as well as remotely using remote PowerShell commandlets. Either way, we can execute PowerShell code in Hyper-V.