PowerCLI: Stats One-liner

Ever wondered how your VM’s are doing ?  What resources are they using, do they have too many CPU’s or too much memory ?

This simple one-liner will give you a list of each of your vms, it will tell you how many cpu’s, the amount of memory, average cpu usage for x amount of days and the average memory usage for x amount of days, how cool is that ?!

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | 
Select Name, Host, NumCpu, MemoryMB, `
 @{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, `
 @{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} `
 | Export-Csv c:\Temp\stats.csv 

Update 03 July 2009: This has been updated as it was not collecting the average over the given timescales, the original code was selecting 1 sample from the timeframe.

Sample output:

Name Host NumCpu MemoryMB Mem.Usage.Average Cpu.UsageMhz.Average
tstVIC01 tstesx02 1 512 25.32 121
tstVCT01 tstesx04 1 1536 14.32 144
tstFOG01 tstesx03 1 2048 12.79 72
ELGMSF01 tstesx01 1 512 20.19 748
tstADC02 tstesx02 1 384 10.32 17
ELGFIL01 tstesx04 1 384 7.19 20
tstADC01 tstesx01 1 384 10.25 16
VISTA tstesx04 1 768 10.99 5
tstWEB01 tstesx01 1 512 49.79 124
ELGXCH01 tstesx02 1 512 15.59 46
ELGADC01 tstesx01 1 512 11.52 19
SmallVM tstesx04 1 512 0 5
tstSQL01 tstesx04 1 512 11.99 58


Alan Renouf has a role of Automation Frameworks Product Manager at VMware responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/vipowershell and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

You may also like...

16 Responses

  1. Jim says:

    Hi Alan,

    i would like to run this scripts to particular one cluster and gather all information for those VMs which are belongs to this cluster.

    Get-Cluster -Name ** | Get-VM | Where {$_.PowerState -eq “PoweredOn”}

    i’m trying add one more code in the beginning with cluster name but its just pull out cluster vm info not a real vm info which i’m looking forward to have using this script

    greatly appropriate if you can provide me a solution.

    Thank you,

  2. Rohit Kakkar says:

    This Script is very good, i am able to get this report.

    but is it possible to get Max. CPU and Max. RAM utilization of VMs since last 12hr or 1 month

  3. Alberto says:

    Hey Alan – great output.
    Is it possible to add memory active, ballooned and cpu ready time to the report??


  4. Larry says:

    ok so this script is the memory and cpu is that what is currently using or assigned, and the average is that percentage of assigned per vm or percentage of host resources? Then is it possible to make it so it shows the average amount rather than percentage and get so emails report?

  5. Larry says:

    How do I get this script to run, all I get is a quick display of PS all in red from what I can see.

  6. ap says:

    Can i get the same code for java or code for listing all the VM’s on a ESX host using Java and VI-sdk

  7. Aureus says:

    I know this is an old script, but I have a question.

    Why do you have “-MaxSamples (12) ” in the script. Wouldn’t that limit the script to only 1 hour of data, rather then the entire day?

  8. kenny says:

    Al – how can i have it so i can run the script daily and it not erase the results from the day before?

  9. James says:


    That would extend the timeframe but wouldn’t you want more samples processed?
    I may be entirely wrong but -MaxSamples (12) over -720 hours wouldnt be very granular to average over.

  10. Kenneth says:

    well technically it is possible to calculate 9am to 5pm, run the script at 5pm with addhours(-9) should give you what you are looking for!
    And run it again at 9am with addhours(-16) and you get the offhours load, might be interesting too !
    Well this should work, unless ofcause I am wrong about the addhours variable :)

  11. Marc says:

    Does this script calculate over 24hours? If I wanted to calculate over 1 month would I have to use the following?

    Get-VM |Where {$_.PowerState -eq “PoweredOn”} |
    Select Name,Host,NumCpu,MemoryMB, @{N=”Cpu.UsageMhz.Average”;E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-720)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, @{N=”Mem.Usage.Average”;E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-720)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} |Export-Csv c:\Temp\stats.csv

  12. Virtu-Al says:

    I’m afraid that is not possible with the Get-Stat cmdlet itself, nor with the underlying QueryPerf (http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.PerformanceManager.html#queryStats) method from the SDK.

    You could get all your statistical data and then use a Select-Object or an If-Then-Else construct on the Timestamp property to filter out the data you want.

    Thanks @LucD – PowerCLI Community answer to a separate post: http://webmail.virtu-al.net/webmail-n/parse.php?redirect=http://communities.vmware.com/message/1325196%231325196

  13. Virtu-Al says:

    Yes this is a percentage, you would obviously need constant monitoring to make that sort of judgement though to ensure you catch trend data.

  14. Marc says:

    Is there a way to calculate average usage during working hours? Say 9am-5pm? This is based on a 24 hour period and could potentially skew the numbers. I would love to see this if it is possible.


  15. Mike says:

    This is great. But I’m not sure if I understand the Mem.Usage.Average, is the Average in percentage format? On your screenshot for an example VM tstFOG01 has 2048 of RAM and on an average is only utilizing 12.79. So if that’s a percentage it’s using under 300MB to round it up. So if that’s the case, the memory is over allocated. So would it be safe to say that 512mb should be enough? I have over 600 VM’s and couple admins that logon and make changes. I’m sure there are a lot of VM’s that are way overallocated when it comes to RAM and I been trying to find an easy way to figure this out. We don’t really have a good tool to easily get the memory utilization. I been using VMware Consolidation by adding VM’s, it’s been working OK but it only gives me a 24hr view.

  16. Herschelle says:

    Love it. I definitely have a use for this, thanks. :)

Leave a Reply

%d bloggers like this: