VMware PowerShell Find VMs with Memory Reservations

VMware PowerShell Find VMs with Memory Reservations

I had a friend message me today asking a if there was a simple way to to use VMware PowerShell to find VMs with memory reservations.

The answer is yes and it’s not that tricky to do, in fact we can do it with a single line of PowerShell.

Memory Reservations

get-VM | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne "0"}

Get all virtual machines, where the virtual machine object has a value in ExtensionData.ResourceConfig.MemoryAllocation.Reservation that is not equal to zero.

After connection and running this in my ‘lab for the day’ the output is as below and will look somewhat similar for your environment;

VMware PowerShell Find VMs with Memory Reservations Memory and CPU Reservations

When seeing how straight forward this was the question came can we capture CPU reservations as well?  Yes, yes we can.

get-VM | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne "0" -or $_.ExtensionData.ResourceConfig.cpuAllocation.Reservation -ne "0"}

Get all virtual machines, where the virtual machine object has a value in ExtensionData.ResourceConfig.MemoryAllocation.Reservation that is not equal to zero or has a value in ExtensionData.ResourceConfig.cpuAllocation.Reservation that is not equal to zero.

The output from which, is the same in my lab, as I don’t have any CPU reservations.

VMware PowerShell Find VMs with Memory Reservations

Can you export to CSV?

This is 2021, where would we be without generating petabytes and petabytes of spreadsheets! Of course we can export this to CSV!

get-VM | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne "0" -or $_.ExtensionData.ResourceConfig.cpuAllocation.Reservation -ne "0"} | Export-csv -path c:\ps\test.csv

Get all virtual machines, where the virtual machine object has a value in ExtensionData.ResourceConfig.MemoryAllocation.Reservation that is not equal to zero or has a value in ExtensionData.ResourceConfig.cpuAllocation.Reservation that is not equal to zero, and export it to a csv file test.csv located in C:\ps.

It’ll give you a file that looks something like this.

VMware PowerShell Find VMs with Memory Reservations

Here’s an output of all the headers it captures.

#TYPE VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl
"Name","PowerState","Notes","Guest","NumCpu","CoresPerSocket","MemoryMB","MemoryGB","VMHostId","VMHost","VApp","FolderId","Folder","ResourcePoolId","ResourcePool","HARestartPriority","HAIsolationResponse","DrsAutomationLevel","VMSwapfilePolicy","VMResourceConfiguration","Version","HardwareVersion","PersistentId","GuestId","UsedSpaceGB","ProvisionedSpaceGB","DatastoreIdList","ExtensionData","CustomFields","Id","Uid"

There we are, a super quick PowerShell oneliner.

Thanks for reading, hopefully this is useful to someone!

Simon