Provisioning a Hyper-V Server Core VM with PowerShell, step by step

Hyper-V Server Core helps maximize hardware resources at the expense of GUI-based management tools, but PowerShell has everything you need.

The best way to maximize the return on your server hardware investment is to achieve the highest possible virtual machine density, and you can do that by running a Hyper-V Server Core deployment of Windows Server. Doing so reduces the host operating system's resource consumption, thereby freeing up additional resources for your virtual machines to use. Of course you have to consider how you will manage the server since the GUI-based management tools won't be installed.

While you can remotely manage Hyper-V Server Core using tools such as the Hyper-V Manager or System Center Virtual Machine Manager, Windows Server 2012 allows you to fully manage Hyper-V using PowerShell.

Creating a new VM

One of the most basic tasks you can perform is creating a new VM. Create a basic VM by using the New-VM command and appending the name of the VM that you want to create. Although this method is simple, the resulting VM probably won't meet your needs. Unless you specify otherwise, the New-VM command will create a VM with the following characteristics:

  • 512 MB of memory
  • A DVD drive
  • No virtual hard disk
  • No network connection
  • One virtual processor

Without a virtual hard disk or a network connection, the virtual machine is relatively useless in most environments. As such, you can either specify the hardware allocations when creating the VM, or you can use various PowerShell commands to provision hardware to the VM after it has been created.

Adding virtual processors

A default VM created through PowerShell only has one virtual processor assigned. You can change the virtual processor assignment by using the following command:

Set-VMProcessor <virtual machine name> -Count 2

The "count" setting specifies the number of virtual processors that you want to assign. There are other command line switches that limit or reserve physical CPU resources or prioritize the VM's CPU usage.

Adding memory to a Hyper-V Server Core VM

The way that you would add memory to a VM varies depending on whether you are using static or dynamic memory allocation. Static memory allocation is simple. For instance, if you wanted to assign 4 GB of memory to a VM named MyVM, you could use the following command:

Set-VMMemory MyVM –Startup 4.0GB

It is also possible to configure a VM to use dynamic memory, but other values must be assigned too, such as the startup memory, minimum memory, maximum memory, priority and buffer. Suppose, for instance, that you wanted to configure a VM named MyVM to start up with 1 GB of RAM and have a maximum of 4 GB of RAM. For this example, we can set the minimum memory to 512 MB. The command for doing so might look like this:

Set-VMMemory MyVM –DynamicMemoryEnabled $True –MinimumBytes 512MB –StartupBytes 1GB –MaximumBytes 4GB –Priority 80 –Buffer 25

Assigning a virtual network adapter

Assigning a network adapter to a VM is a matter of determining the name of the virtual switch linked to the network adapter and then connecting the VM to the virtual switch.

The first step is to retrieve the name of the VM's virtual network adapter and assign it to a variable named $VMNic using the following command:

$VMNic = Get-VMNetworkAdater –VMName <your virtual machine name>

Next, you must determine the virtual switch name. You can determine virtual switch names by using this command:

Get-VMSwitch | Select-Object Name

Next, you must connect a virtual network adapter to the virtual switch. Suppose, for instance, that the virtual switch name was Intel Gigabit Adapter. In this situation, the command would look like this:

Connect-VMNetworkAdapter –VMNetworkAdapter $VMNic –SwitchName "Intel Gigabit Adapter"

Assigning a virtual hard disk

You can create a virtual hard disk by using the New-VHD cmdlet, even if you want to create a VHDX-based virtual hard disk. Suppose, for instance, that you want to create a 100 GB virtual hard disk named MyDisk.VHDX. To do so, you would use this command:

New-VHD D:\VHDs\MyDisk.VHDX –Size 100GB

The tricky thing about assigning a virtual hard disk to a VM is that you have to pick a port that isn't in use. For a brand new VM without a virtual hard disk, IDE port 0,1 is usually a safe bet. Therefore, if you wanted to add MyDisk.VHDX to MyVM, you could use this command:

Add-VMHardDiskDrive MyVM IDE 0 1 –Path D:\VMs\MyDisk.VHDX

It's easy to create VMs and modify hardware allocations throughout PowerShell. In fact, you can do anything through PowerShell that you can do through the Hyper-V Manager. For instance, if you want to start the VM that you just created, you could do so by entering Start-VM MyVM.

Dig Deeper on Virtual machine provisioning and configuration