Creating Virtual Machines with Hyper-V and PowerShell

If you want to learn how to hack, you’ll need some machines that you can legally hack and experiment with, without risk of doing harm. Rather than use physical machines, which most of us are unlikely to have lying around, you can use virtual machines. A virtual machine (VM) runs inside an existing operating system, such as Windows, and shares its resources. You could have a host OS of Windows 10, running several VMs, each using a chunk of the host OS’s physical memory, storage capacity and CPU time.

In this guide, I’ll show you how to use Microsoft’s Hyper-V, which is included with Windows 10 Professional, and recent versions of Windows Server, in order to set up a VM. You will then be in a position to deploy more VMs, and build a virtual environment.

If you don’t already own a copy of Windows 10 Pro (or Server) and are reluctant to spend money, an alternative to Hyper-V is VirtualBox VirtualBox. VirtualBox is free open-source software maintained by Oracle. I will be covering VirtualBox in a separate guide.

Note: You can obtain a standalone version (a free non-expiring evaluation edition) of Hyper-V from Microsoft’s evaluation centre but you’ll be limited to a command-line interface (not necessarily a bad thing). This standalone edition of Hyper-V will need to be installed as the base operating system, to a dedicated machine, so you’ll need a spare desktop or laptop lying around, that meets the system requirements. Advice to beginners: Use VirtualBox if you don’t already have Hyper-V!

Tutorial Requirements

In order to complete this tutorial, you will need to be running Windows 10 Pro or have some other means of using Hyper-V. You’ll also need at least 6GB of RAM to run a single VM alongside your host OS.


Before you proceed, start downloading the following files ready for use later:

Installing Hyper-V on Windows 10 Pro

Hyper-V does not need to be downloaded, it just needs to be enabled. You can do this using PowerShell, a powerful object-oriented scripting language that allows for command-line interaction. Launch PowerShell by searching for it in the start menu (press the Windows key, and then start typing PowerShell). Make sure you run PowerShell with Administrator privileges by right-clicking the PowerShell icon, and selecting run as Administrator. Then, at the PowerShell prompt, type the following command:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

You could copy and paste that command, but I recommend typing commands like this. Typing will help you get used to the command-line, and the quirks of the particular shell interpreter or scripting language you’re using. Once the installation is finished, you’ll be prompted to restart your machine. Do that, and then you can begin creating your first VM.

Creating Virtual Machines with PowerShell

Note: You can use Hyper-V Manager, a desktop app, to create and manage VMs but I’m going to show your how to do it using PowerShell.

After your system reboots, start PowerShell again with Administrator privileges as you did before, and use the following command to list available virtual switches:

Get-VMSwitch  * | Format-Table Name

It is likely that you will see only a single switch in the list that is output:

Default Switch

Virtual switches are used by Hyper-V to allow networking between VMs (as well as access to your local area network and beyond) and work like physical network switches. They only real difference is that a virtual network switch does not physically exist in the real world, it exists as software running on your Hyper-V host.

The next command, will create a new VM named windows2019 and give it 4GB of random access memory (RAM). The command specifies that the boot device for this VM will be a virtual hard disk (VHD) called windows2019.vhdx, that is 20GB in size. The Generation option is set to 1 and the virtual switch to be used is Default Switch. By setting the generation to 1, this VM will act more like a machine that has a basic input output system (BIOS), as opposed to one that has a unified extensible firmware interface (UEFI).

New-VM -Name windows2019 -MemoryStartupBytes 4GB -BootDevice VHD -NewVHDPath .\VMs\windows2019.vhdx -NewVHDSizeBytes 20GB -Path .\VMData -Generation 1 -Switch "Default Switch"

Note: The amount of memory you assign to the VM must be physically present on your host operating system, and be in excess of what your OS is already using, so be careful when setting this option.

As a result of the previous command, you should see something like the following output:

Name        State CPUUsage(%) MemoryAssigned(M) Uptime   Status             Version
----        ----- ----------- ----------------- ------   ------             -------
windows2019 Off   0           0                 00:00:00 Operating normally 9.0

You now have a VM, but you will be unable to install from an ISO, since there is no optical drive. Use the next command to add a DVD drive and specify the path to the ISO you downloaded earlier (the path will be different in your case):

Add-VMDvdDrive -VMName windows2019 -Path "C:\Users\Jim\Downloads\SERVER_EVAL_x64FRE_en-us_1.iso"

Note: To get help on a particular PowerShell command (a PowerShell command is called a cmdlet) use the cmdlet, Get-Help. For example, Get-Help Add-VMDvdDrive.

You will now need to make changes to the boot order of your VM, so that it boots from DVD. You can check the boot order using Get-VMBios -VMName windows2019, which will result in output like the following:

VMName      StartupOrder                            NumLockEnabled
------      ------------                            --------------
windows2019 {IDE, CD, LegacyNetworkAdapter, Floppy} False

Note that there is no DVD option here, but moving CD to the start of the boot order will work just fine. Use the following command to do just that:

Set-VMBios -VMName windows2019 -StartupOrder CD,IDE,LegacyNetworkAdapter,Floppy

If you run your previous Get-VMBios command again, you should see that the order has been changed, like this:

VMName      StartupOrder                            NumLockEnabled
------      ------------                            --------------
windows2019 {CD, IDE, LegacyNetworkAdapter, Floppy} False

You are (finally) ready to start your VM. Do this with the Start-VM cmdlet, like so:

Start-VM -Name windows2019

To actually view and interact with the VM, you’ll need to run vmconnect. Just type vmconnect at the prompt and hit enter. Doing so presents a dialogue box, allowing you to select a Server — localhost in this case — and your VM (windows2019) from the Virtual Machine drop-down box. Click OK with these selected and you’ll and click OK. From here you will need to follow the installation procedure as with a physical machine. The great thing is, you can have this running in the background, whilst getting on with other tasks.

You can view a list of your VMs by typing Get-VM, which will present a list like so:

Name            State   CPUUsage(%) MemoryAssigned(M) Uptime           Status             Version
----            -----   ----------- ----------------- ------           ------             -------
windows2019     Running 0           4096              00:02:37.5460000 Operating normally 9.0

Building a Virtual Environment

You have one VM, but you’ll need several in order to mimic an internal network. The number of hosts you’re able to run simultaneously will depend on the amount of RAM you have to spare. Your host OS (Windows 10) will need to have enough RAM to continue chugging away too. I’ll cover adding more hosts (both Windows and Unix-based) and networking them in a future article.