More PowerCLI basics – Host operations


On my quest to learn more about PowerCLI, I have been playing around with some more cmdlets in my lab. As a simple task, I have figured out how to tell ESX or ESXi hosts to enter and exit maintenance mode. Here’s how we do this. First of all ensure you are connected to your vCenter server instance using Connect-VIServer ServerName.


Enter maintenance mode:

Set-VMHost ESXi-01.noobs.local -State "Maintenance"


Exit maintenance mode:

Set-VMHost ESXi-01.noobs.local -State "Connected"


Set host to “disconnected” state:

Set-VMHost ESXi-01.noobs.local -State "Disconnected"


So now that we know the basics of setting the state of a VMware host, how about we get slightly more technical and perform one of the above operations on a bunch of hosts in one go? Powershell / PowerCLI is all about automation after all! Note that in the following script, I have also include a simple “if / else” statement to prompt the user running the script manually as we are about to send all ESX(i) hosts into maintenance mode! Use this at your own risk of course, it is just for demonstration purposes. You may want to modify to select hosts to enter maintenance mode on certain criteria. For example, all hosts in a particular cluster, or all hosts with a certain property. Here is the script I would use to perform the operation on all the ESX(i) hosts found in vCenter:


$VCServer = “yourvcservername”
Connect-VIServer $VCServer
$confirm = Read-Host “Are you sure you want all hosts to enter maintenance mode? (type yes to continue) “
if ($confirm -eq "yes")
	Get-VMHost | Set-VMHost -State "Maintenance"
	"Script aborted (you didn't confirm by typing yes)"


Once the script is executed, you should get a progress indicator whilst hosts are being dealt with. Afterwards you’ll get some output from each host listing its relevant Connection Status and statistics. Like so:


In the above example, we set some variables, and use some basic logic checking with an IF ELSE statement and an equal to (-eq) operator. We also see how to perform a few operations on ESX or ESXi hosts. I hope this helps anyone starting out with PowerCLI. Please do leave any comments, suggestions or improvements in the comments section!



  1. Hello,

    The syntax for the evacuate parameter is -evacuate:$false

    Set-VMHost $host -State “Maintenance”-evacuate:$false


  2. Hi Tom,

    Indeed, it seems as though I also had trouble using the -Evacuate switch. It just wouldn’t accept $false or even $true when I tried it. Luckily for us there is a great little VMware utility called Project “Onyx” that acts as a proxy between the vSphere Client and your vCenter server, capturing all the commands behind tasks you initiate. I used this to figure out what happens when you tell a host to enter maintenance mode without moving suspended or powered off VMs off the host. This is what came up. I tested this manually in PowerCLI and indeed it does enter your host into maintenance mode, leaving the VMs where they are:

    $timeout = 0
    $evacuatePoweredOffVms = $false
    $myVMHost = "esxi-02.noobs.local"
    $ESXiHost = Get-VMHost $myVMHost | Get-View
    $ESXiHost.EnterMaintenanceMode_Task($timeout, $evacuatePoweredOffVms)

    If you do want the VMs evacuated off the host, then you just set $evacuatePoweredOffVms to $true.

    Hope that helps! 🙂


  3. Hello,

    I’m working on a script to suspend VM’s, put hosts into maintenance mode, and then shut down the hosts.

    The problem is that when I put the hosts into maintenance mode they vMotion all the VM’s to another host. I don’t want this. When I researched the set-vmhost command I see there is supposed to be a switch called -Evacuate which controls whether VM’s are vMotioned or not. I’ve tried to use -Evacuate $false but I get an error.

    Any suggestions to prevent the host from vMotioning my VM’s?


Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.