Random Post: VAAI and supportability
RSS .92| RSS 2.0| ATOM 0.3
  • Home

    PowerCLI – Generate Count of Running VM’s

    If you are like me, you have virtual machines in different states in your virtual environment.  Running, paused, or powered-off.  If you have ever been asked “How many VM’s do we have,” and you know the right answer is technically not what vCenter lists as total VM’s, run the following PowerCLI script:

    (get-vm )| %{
      $vm = $_
      if( get-vmguest -VM $vm.Name |where-object {$vm.State -eq "Running"}){
    echo $vcounter

    Let’s break down line 5, where all of the magic happens.  We are running get-vmguest on the current VM pulled from get-vm on line 3, and then determine if it’s state is “Running.”  PowerCLI and vCenter differ in how they display the state of a VM: “Running” or “Not-Running” in PowerCLI vs “Powered On” or “Powered Off” in vCenter.

    Voila, you now have the count of virtual machines that are Powered-On and theoretically doing work.

    3 responses to “PowerCLI – Generate Count of Running VM’s”

    1. Daniel says:

      Excellent code, changed Running to NotRunning, and it did my job 🙂

    2. Burqa says:

      Hi there,

      Checking for the state to be equal to ‘Running’ is not enough.

      Right now I have a running vm which doesn’t have VMware Tools installed
      and it’s state is ‘NotRunning’

      What you should be checking for is ‘PowerState’

      PowerCLI C:\> get-vm -name centostest |fl

      Name : centostest
      PowerState : PoweredOn

      Great blog btw 🙂

    Leave a Reply

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