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

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


Out of all of the physical hardware resources that are commonly assigned to virtual machines, perhaps none are as critical as memory. With that in mind, I wanted to take a look at how memory management works through Windows PowerShell.

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.

Creating a Memory Usage Report

Because physical memory is a crucial, but limited hardware resource, it is important to be able to determine how much memory has been allocated to your virtual machines. Although it is relatively simple to do this through the Hyper-V Manager, the Hyper-V Manager does not offer any sort of reporting function. Fortunately, we can build our own memory usage report directly through Windows PowerShell.

The command that is used to check to see how much memory a virtual machine is using is Get-VMMemory. The syntax for this command is as follows:

Get-VMMemory –VM”<virtual machine name>” –Server “<Hyper-V host server name>”

Technically, the Server switch is a required parameter (at least according to the documentation), but if you omit it then the Get-VMMemory command will query the local server by default. Normally you would specify a single server name, but it is possible to specify multiple server names. If querying multiple Hyper-V hosts you must specify the full server names. You cannot use wildcards.

The virtual machine name parameter is a completely different story. This parameter is not required. If you omit the virtual machine name then PowerShell will show you the memory allocations for every virtual machine on the specified host server. If you want to narrow down the list, you can specify a single virtual machine or multiple virtual machine names. You can even use partial server names with wildcard characters.

If you enter the Get-VMMemory command by itself with no parameters specified, the command will display the memory allocation for each virtual machine that is hosted on the local server. You can see what this looks like in Figure A.

Figure A: The Get-VMMemory command returns memory allocation information for each virtual machine on the local server.

As you can see in the figure above, the command lists the virtual quantity, the limit, and the reservation for each virtual machine. In case you are wondering, the reason why the virtual quantity, limit, and reservation values are all identical on each of the virtual machines in the figure is because I do not have Hyper-V’s memory over commitment feature enabled on my server. Otherwise, these values would differ.

Although it is handy to be able to view the virtual quantity, limit, and reservation data for each virtual machine, you can actually use the Get-VMMemory to acquire much more memory related data. In order to do so, you will need to know the names of the properties that are supported by the Get-VMMemory cmdlet.

To see a list of all of the available properties, enter the following command:

Get-VMMemory | Get-Member –MemberType *Property | Select-Object Name

You can see the results of this command in Figure B.

Figure B: You can view a list of the properties that are available for the Get-VMMemory command.

As you can see in the figure, VirtualQuantity, Limit, and Reservation are among the properties listed, but there are many other properties as well. You can use the Get-VMMemory command to display any combination of these properties. Suppose for example that you wanted to see the VMElementName, Address, and AllocationUnits. You could view those parameters by entering the following command:

Get-VMMemory | FT VMElementName, Address, AllocationUnits

You can see what this looks like in Figure C.

Figure C: You can view any of the properties for the Get-VMMemory command.

Creating a Report

Earlier I mentioned that you could use the Get-VMMemory command to create a report. I have already shown you some techniques for extracting the desired memory allocation and usage information, but so far all we have done is to display that information in the console window. If you want to save the information so that it can be used later on, then one way of doing so is to export the Get-VMMemory cmdlet’s output to an HTML file.

You can accomplish this by using the following command:

Get-VMMemory | ConvertTo-HTML | Out-File C:\temp\Test.HTM

As you can see in Figure D, this command produces no visible output. That’s because the command’s output is being redirected to an HTML file named C:\temp\test.htm. You could use Windows Explorer to locate and open the file, but as an alternative you can open the file without ever leaving PowerShell. Simply enter the following command:

Figure D: The command produces no visible output.

Invoke-Expression C:\temp\test.htm

If you look at Figure E, you can see that this command launches Internet Explorer and displays the HTML file that we have created. You will probably also notice that the HTML report looks a lot different from the report shown in Figure A, even though both commands used the Get-VMMemory command without any additional parameters. In fact, if you look at the bottom of the figure you will notice that the scroll bar indicates that there is a lot more data displayed than what will fit on the screen.

Figure E: This is what an HTML report looks like.

Being that there is so much memory data to display, it might be more helpful to create a CSV file so that the data can be viewed as an Excel spreadsheet rather than as a Web page. Fortunately, PowerShell has this capability as well. The command that you would use to do so is nearly identical to the command used to create an HTML report. This time however, instead of using ConvertTo-HTML we are using ConvertTo-CSV. The full command is:

Get-VMMemory | ConvertTo-CSV | Out-File C:\temp\test.csv

After the CSV file is created, you can double click on the file to open it in Excel. However, it will not display correctly if you do. In order to get the file to display properly, you must open Excel and then open the file. Doing so will cause Excel to launch the Text Import Wizard. You should tell the wizard to start importing the file with Row 2, and to treat the data as delimited. You can see an example of this in Figure F.

Figure F: The import should start with row 2.

On the following screen, you must set the delimiter option to Comma. After doing so, click Finish. The data will be imported, as shown in Figure G.

Figure G: The data has been imported into Excel.


So far I have shown you how to extract virtual machine memory configuration data from PowerShell. In the next article, I will build on this technique and show you how to modify memory allocations.

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

About The Author

1 thought on “Managing Hyper-V From the Command Line (Part 4)”

  1. Hello thanks for post. I like it. just have one question. what is you want to view the maximum amount of memory used by the VM. Meaning that if it was assigned 4GB it does not mean it will use it completely all the times. sometimes it will use less. I am asking for a PowerShell command that will retrieve the maximum account the VM has used. This would help reduce memory in the VMs that are not using them completly and then assign it to another VM that needs them the most.


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