Looking at Memory Usage on Hosts when Creating a New VM

In my house I have two Hyper-V servers running multiple virtual machines with dynamic memory enabled.  This can make it a bit tricky when I want to create a new virtual machine – and I need to figure out the best server to use.  Thankfully – this little bit of PowerShell comes to the rescue:

 “Hyper-V-1″, “Hyper-V-2″ | %{“Memory Available on ” + $_ + ” : ” + (“{0:N2}” -f (((get-vmhost $_).MemoryCapacity / 1GB) – ((get-vm -computername $_ | measure MemoryAssigned -sum).sum / 1GB))).ToString() + ” GB”}

“Hyper-V-1″ and “Hyper-V-2″ are the names of my hosts.  This snippet gets the total memory in each host, subtracts the memory currently being used by virtual machines, and shows the results.  Like this:

So I can see that Hyper-V-2 has more memory available right now.

This is obviously a simple approach (it does not account for changing memory demand) but it is quick and easy to do.

Cheers,
Ben