How to shutdown all VMs in an ESXi host using PowerCLI

There are two ways. You can connect to a host or you can connect to a vCenter.

Connecting to individual host

Open VMware vSphere PowerCLI console

#Connect to the host
Connect-VIServer -Server hostname.example.com

#Get all VMs from the host, filter those powered on and shut them down
Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false

Connecting to vCenter

If you connect to vCenter, you have more options. You can target a single host, a cluster, a datacenter or the whole vCenter.

If you need to shutdown all VMs in the vCenter, virtu-al.net has a nice script.

First connect to your vCenter

Connect-VIServer -Server vCenter.example.com

Shutdown all VMs in a host

#Get all VMs from the host, filter those powered on and shut them down
Get-VMHost "hostname.example.com" | Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false

Shutdown all VMs in a cluster

#Get all VMs from the cluster, filter those powered on and shut them down
Get-Cluster “Cluster1” | Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false

Shutdown all VMs in a datacenter

#Get all VMs from the datacenter, filter those powered on and shut them down
Get-DataCenter “DC1” | Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false

For one reason or the other, VMs refuse to shutdown gracefully. If you have a stuck VM, use Stop-VM to force poweroff.

Eg-

Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Stop-VM -Confirm:$false
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s