Managing Hyper-V From the Command Line (Part 6)

If you would like to read the other parts in this article series please go to:


In the previous article in this series, I showed you how to create a virtual machine. The problem is that the virtual machine that I created lacks sufficient resources to make it useful. That being the case, I want to spend this article showing you how to allocate various resources to a virtual machine. I will start out by showing you how to create a virtual hard disk and then later we will add other resources such as CPU cores and network adapters.

Before I Begin

Before I get started, I want to quickly mention for the benefit of anyone who is just joining this series that the commands that I will be discussing are not native to Hyper-V. They are a part of a Hyper-V management library. Furthermore, this library must be imported into PowerShell each time that you plan on using any Hyper-V related commands. The import command is:

Import-Module “C:\Program Files\Modules\Hyperv\Hyperv.psd1”

The full instructions for downloading and installing the Hyper-V management library are provided in Part 1 of this series.

Incidentally, manually downloading the Hyper-V module for PowerShell and manually importing it will not always be necessary. The Hyper-V module is going to be included in Windows Server 8.

Virtual Hard Disks

The virtual machine that we created in the previous article does not have a virtual hard disk file associated with it, so the first order of business is to create a virtual hard disk file. This is accomplished via the New-VHD cmdlet. There are a number of different parameters that can be used with this cmdlet including:

VHDPaths – This parameter specifies the path and name of the virtual hard disk file.

Size – The size parameter reflects the size of the virtual hard disk that is to be created. It is worth noting that the size is expressed in bytes.

ParentVHD – The ParentVHD switch is only used if you are creating a differencing disk. Since we are creating a primary virtual hard disk, this parameter will be unnecessary.

Server – The Server parameter allows you to express which Hyper-V the operation will be run against. If you omit this parameter then the current server will be used.

Fixed – Using the Fixed parameter tells Hyper-V to create a fixed length virtual hard disk. If this parameter is omitted then a dynamically expanding virtual hard disk is used instead.

Wait – The Wait parameter should always be used in conjunction with the Fixed parameter. It forces the cmdlet to wait for the fixed length virtual hard disk to be created.

Force – The Force parameter is only used in scripted commands. It causes the cmdlet to carry out the action without any user intervention.

So now that I have shown you the various parameters that can be used with the New-VHD cmdlet, let’s go ahead and create a virtual hard disk. To do so, I will be using the following command:

New-VHD –VHDPaths “F:\Windows Server 8 Beta\NewVHD.vhd” –Size 16106127360

When you run this command, you will receive a warning telling you that the job is still running in the background. However, creating a dynamically expanding virtual hard disk file (which is the command’s default behavior) usually only takes a few seconds, so this warning message can be disregarded.

So what does this command get you? Well it creates a dynamically expanding 15 GB virtual hard disk file named NewVHD.vhd that is located in the F:\Windows Server 8 Beta folder.

So it sounds like we are all set, right? Not quite. Even though we have created a new virtual hard disk file from the command line, that virtual hard disk is not yet bound to a virtual machine. At this point the file that we created is nothing more than an orphaned VHD file.

To resolve this situation we have to use the Add-VMDisk cmdlet. Actually you will have to use a series of two commands. The first command assigns the virtual machine to a variable (VM$). That command is:

$VM=Get-VM “Windows Server 8 Beta” –Server Hyper-V

In this command I am simply specifying the name of my virtual machine (Windows Server 8 Beta) and the name of my host server (Hyper-V).

The second command assigns the virtual hard disk file to a specific controller ID and to a specific LUN on the virtual machine. That command is:

Add-VMDisk –VM $VM –ControllerID 0 –LUN 1 –VHDPath “F:\Windows Server 8 Beta\NewVHD.vhd”

After completing this command you can verify the operation by using the Get-VMDisk cmdlet along with the name of the virtual machine. You can see an example of this in Figure A.

Figure A: You can use the Get-VMDisk cmdlet to verify that the virtual hard disk has been assigned to a virtual machine.

Adding Network Support

Now that we have created and assigned a virtual hard disk to our virtual machine, we need to add network connectivity. This process isn’t quite as cut and dry as it might at first seem.

As you probably already know, each virtual NIC must be connected to a virtual switch. Virtual switches are typically bound to physical network adapters. Therefore, the first step in the process is retrieving the name of a virtual switch to use with our new virtual NIC.

I recommend that you start out by entering this command:


The command will display a list of every virtual machine on the server as well as which virtual switch is being used. The problem is that the virtual switch name usually gets cut off. Therefore, you should use the list to find another virtual machine that is connected to the virtual switch that you want to use. After doing so, enter this command:

Get-VMNic <vm name> | FL

In the command above, you should replace the <vm name> with the name of a virtual machine. For example, on my system I used a virtual machine named Storage, so the command that I entered looked like this:

Get-VMNic Storage | FL

This command returns a great deal of information about the specified server’s virtual switch. Be sure to make note of the virtual switch name. In my case the switch name was Local Area Connection 2 – Virtual Network.

Now that you know the name of the virtual switch, it is time to create the virtual NIC. To do so, you will use the Add-VMNic command. This command has a tremendous number of parameters that can be used, but most are optional.

In my case, I am creating a virtual NIC for the virtual machine named Windows Server 8 Beta using a virtual switch named Local Area Connection 2 – Virtual Network. The command that I would use looks like this:

Add-VMNic –VM “Windows Server 8 Beta” –VirtualSwitch “Local Area Connection 2 – Virtual Network”

You can verify the operation’s success by using the Get-VMNic command as shown in Figure B.

Figure B: You can verify the operation’s success by using the Get-VMNic command.


In this article, I showed you how to add a virtual hard disk and a virtual NIC to a virtual machine. In Part 7, I will continue the discussion by showing you how to allocate additional hardware to the virtual machine.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top