Virtualization has given us a world of ease when it comes to IT/server administration.  Sometimes, it’s so easy to shut down and start up VMs that we forget the fact that these are actual servers. Under normal circumstances, an administrator doesn’t just yank the power cord on their Dells, or hold down the power button on the HPs without first making sure the guest OS has been shut down gracefully.  With the hypervisor running on the hardware and multiple VMs living on those hypervisors, proper administration can get sloppy, and when it does, it creates a slew of problems for both the admin and the customer(s).

Be Graceful

Unless your system is hung, frozen, or infected with a virus… it’s far better to shut down all of your VMs gracefully before powering down the host system.  If you are only running 2 or 3 VMs, this is a piece of cake and can be done fairly quickly.  On the flip side, if you are an enterprise administrator and have over 100 VMs, it can be a painstaking process if you have to do it individually.  Thankfully, VMware’s PowerCLI is an incredible tool that will allow you to automate the process of gracefully shutting down your VMs, and bringing them back up when the host system is back online.

Recently I used PowerCLI to shut down a bunch of VMs, and start them back up for a customer, and it saved them a bunch of time.  As a Sr. Consultant, this was vital to teach the customer how to treat their VMs in a graceful manner, seeing as before they were simply cold shutting down their host systems at the end of every day.  The customer appreciated the script, and now have it as an executable shortcut on their desktop so the admin can simply click on the script to run it.  Here’s the script, you’ll have to fill in your environment variables where required, follow the steps as I’ve laid them out:

  1. The first thing you need to do is download VMware’s PowerCLI (free download) and connect it to your vCenter server which you use to manage the VMs you want to shut down/start up.  Use the command, “Connect-VIServer “
  2. Next, if you want to shut down all of your VMs being managed by vCenter type the following commands:
    1. $vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}
    2. $vmservers | Shutdown-VMGuest   (Note: this line when executed will prompt you to give confirmation before shutting down, this is a good failsafe).
  3. Those steps are great if you want to shut down all your guest VMs, if you want to shutdown only a subset of VMs, you can type the following command, $vmservers=Get-VM -location “XYZ_vm_folder” | Where-Object {$_.powerstate -eq ‘PoweredOn’}

That’s all there is to it!  You can save the script as an executable file wherever you want it to be run from, or a share on the network so administrators can pull it down from any server they are logged into on the same network as vCenter.  This script is a real time saver, be sure that the users/administrators that are using this script know what they are doing and have the permissions to run the script.  If you have any questions, or need any help with this script, feel free to reach out.

Greg W Stuart
Greg is the owner and editor of He's been a VMware vExpert every year since 2011. Greg enjoys spending time with his wife and 3 kids. He works as a Sr. Consultant at VMware and resides in Northern Virginia, 15 minutes west of Washington DC.

Leave a Reply

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