Change Hyper-V VM Memory “as fast as possible” – 60 Second Solutions

Hyper-V is amazing – you can do pretty much anything with a VM when it’s online now – even move it to an entirely new host with no shared anything.

Apart from change the amount of memory in the VM!

(Yes, I know about dynamic memory – still don’t like it. I’ve had too many instances whereby VMs don’t claim as much ram as they need – even when the host has tones of ram free – and other cases where VMs just balloon, consuming far too much but not actually using/needing it!)

The quickest way to change the amount of static ram in an Hyper-V VM is to turn it off, go into settings -> memory -> update the ‘startup’ ram box and start the VM again.. or is it?

PowerShell to the rescue!

Of course, we can now automate this procedure in PowerShell:

$vm = get-vm “local VM name here“; set-vm $vm -StaticMemory -MemoryStartupBytes 4096MB; start-vm $vm

Get a VM on the local computer and store it as a variable, turn dynamic memory off if it’s on, set to 4096MB of ram and start it again.

Things get a little trickier when you’re dealing with a VM in a cluster:

$cvm = get-clusterresource -cluster “cluster name here” | where name -eq “Virtual Machine VM NAME HERE” | get-vm; Stop-VM $cvm; set-vm $cvm -StaticMemory -MemoryStartupBytes 4096MB; start-vm $cvm

Get all the resources in the given cluster, match virtual machines where the name is XXXX and just get the VM. Store this in a variable. Shutdown the virtual machine, turn dynamic memory off if it is on, set the ram and switch it back on again!

You can pair this command with the ‘wait-until’ script located here, to perform the shutdown / restart operation at a more convenient time.

I’ve only tried this on a Server 2012 R2 cluster/Hyper-V instance, so YMMV!

Leave a Reply

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