Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have ssh login to an ESXi 5 host.

All the guests have vmware tools running, so they can be shut down by the host properly.

I would like an equivalent for this menu option in vSphere client:

enter image description here

It shuts down all the guests automatically (if they have vmware tools running) and then the host itself.

Is there such a "smart" shutdown all command on the ESXi 5 commandline?

share|improve this question
Exact duplicate of – gWaldo Jan 14 '12 at 3:13
Not really a duplicate.This question asks for host shutdown as well as vms while the link above deals with vms only.To shutdown both, this script is one of the possible solutions:… – Sergei Jan 15 '12 at 12:25
@Sergei - That seems to be exactly what he's looking for -- Please post it as an answer :-) – voretaq7 Jan 25 '12 at 23:19
Hi, I think the question was closed that is why i just commented.If you mark my comment as useful, this is good enough for me , thank you :) – Sergei Jan 27 '12 at 19:10
up vote 2 down vote accepted

I think I found an answer. This script could do it:

Please note the part in line 17ff where it waits for the VMs to be Shutdown cleanly

Thank you Sergei!

    Connect-VIServer MyVIServer
3   # Get All the ESX Hosts
4   $ESXSRV = Get-VMHost
6   # For each of the VMs on the ESX hosts
7   Foreach ($VM in ($ESXSRV | Get-VM)){
8       # Shutdown the guest cleanly
9       $VM | Shutdown-VMGuest -Confirm:$false
10  }
12  # Set the amount of time to wait before assuming the remaining powered on guests are stuck
13  $waittime = 200 #Seconds
15  $Time = (Get-Date).TimeofDay
16  do {
17      # Wait for the VMs to be Shutdown cleanly
18      sleep 1.0
19      $timeleft = $waittime - ($Newtime.seconds)
20      $numvms = ($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count
21      Write "Waiting for shutdown of $numvms VMs or until $timeleft seconds"
22      $Newtime = (Get-Date).TimeofDay - $Time
23      } until ((@($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime)
25  # Shutdown the ESX Hosts
26  $ESXSRV | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)}
28  Write-Host "Shutdown Complete"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.