Using PowerShell to configure Dynamic Memory

by Mitch Tulloch [Published on 2 July 2014 / Last Updated on 2 July 2014]

A tip on how to use the -Passthru parameter to more easily modify Dynamic Memory settings on Hyper-V hosts

One change in the way that Dynamic Memory can be configured in Windows Server 2012 is that now you can modify the Maximum RAM and Minimum RAM settings while the virtual machine is running. In Windows Server 2008 R2, the Maximum RAM setting could be modified only when the virtual machine was in a stopped state. This change gives you a new way of quickly provisioning more memory to a critical virtual machine when needed.

You can also use the Set-VM cmdlet to enable and configure Dynamic Memory for a virtual machine using Windows PowerShell. For example, let’s say you wanted to enable Dynamic Memory for a virtual machine named SRV-B that is running on HOST4 and configure the maximum RAM as 4 GB. To do this, you first have to stop the virtual machine because you cannot enable or disable Dynamic Memory while the virtual machine is running. You can use the Stop-VM cmdlet to do this as follows:

PS C:\> Stop-VM -Name SRV-B -ComputerName HOST4

Next you can use the Set-VM cmdlet to enable Dynamic Memory for the virtual machine and set the maximum RAM to 4 GB as follows:

PS C:\> Set-VM -Name SRV-B -ComputerName HOST4 -DynamicMemory -MemoryMaximumBytes 4GB

Now you can use Start-VM to restart the stopped virtual machine:

PS C:\> Start-VM -Name SRV-B -ComputerName HOST4

Let’s now say that you decide later that 3 GB would be a better value for maximum RAM than 4 GB. By using the –Passthru parameter, which specifies that an object is to be passed through to the pipeline, you can make the change and verify the result using a single Windows PowerShell command as follows:

PS C:\> Stop-VM -Name SRV-B -ComputerName HOST4 -Passthru | Set-VM -DynamicMemory `
-MemoryMaximumBytes 3GB -Passthru | Start-VM -Passthru | Get-VM | `
Format-List DynamicMemoryEnabled,MemoryMaximum

DynamicMemoryEnabled : True
MemoryMaximum        : 3221225472

The above tip was excerpted from Mitch Tulloch's book Training Guide: Installing and Configuring Windows Server 2012 from Microsoft Press.

Mitch is a nine-time recipient of the Microsoft Most Valuable Professional (MVP) award and a widely recognized expert on Windows administration, deployment and virtualization.  For more information see http://www.mtit.com.

See Also

Featured Links