Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware

Hi All,

There may be times when you have the need to shut down multiple VMware Guest such as when you need to perform maintenance. Rather than go and visit each virtual machine individually, you can use the following few lines to shut down the guest operating systems gracefully as long as VMware Tools is installed. To do anything with PowerShell in VMware you will need to install “VMware vSphere PowerCLI”. You will then need to open PowerCLI connect to your VMware vCenter server by typing “Connect-VIServer vCenterServerName”.

To shutdown all VM guest managed by vCenter you can type the following 3 lines:

$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}

$vmservers | select Name | export-csv c:\MyScripts\servers.csv -NoTypeInformation

$vmservers| Shutdown-VMGuest

The script above will do 3 things. The first line will get all VMs that are currently powered on. The second line will log all the powered on VMs in line 1 and log them to a csv file servers.csv located in c:\MyScripts. The reason I log these machine names to a csv file is so when it’s time to turn the machines back on, I don’t inadvertently turn on any VMs that were turned off for whatever reason before I began the shutdown. The third line will shutdown the Vmware guests. The third line will ask you for confirmation before doing anything. If you don’t want to be asked for confirmation, you can simply add “-Confirm:$false” as shown below:

$vmservers | Shutdown-VMGuest -Confirm:$false

Now let’s say you only want to shut down only a subset of machines. If you have your VMs organized by folders in vCenter this will be easy. All you need to do is add the location parameter to the first line with the name of the folder. In the example below, I’ve changed the first line to get all of the VMs located in the “My Lab” folder.

$vmservers=get-vm -location “My Lab” | Where-Object {$_.powerstate -eq ‘PoweredOn’}

So basically, if you wanted to run a shutdown on all VMware guest in the “My Lab” folder only and not receive any confirmation before the shutdown begins, you would run the 3 lines as shown below.

$vmservers=get-vm -location “My Lab” | Where-Object {$_.powerstate -eq ‘PoweredOn’}

$vmservers |select Name | export-csv c:\MyScripts\servers.csv -NoTypeInformation

$vmservers | Shutdown-VMGuest -Confirm:$false

If you needed a way to bring all the VMware guest that you shutdown, you could use the server.csv file created previously using the following 2 lines:

IMPORTANT NOTE: You need to be careful with this. Depending on your environment, bringing up too many VMs at one time could wreak havoc on your storage or on your environment in general. You would be best off only limiting the amount of servers in the csv file to just a few so you don’t have any problems.

$servers = import-csv C:\MyScripts\servers.csv | select -ExpandProperty name

Start-VM -VM $servers

I hope this helps. This will give you a quick and easy way to shutdown multiple virtual machines. If you have any questions or feedback, please leave a comment.

Related Links:

Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

Using PowerShell to View  Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File


2 Responses to Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware

  1. Scott Greymont says:

    Great bit of code
    This is a lot more graceful than exporting the list and shutting down based on the targeted list.

  2. Tony says:

    This was very helpful, thanks for the post. Would you happen to have a script for ‘vMotion bulk VMs to a new cluster’ with storage staying as is? Thank you.

Leave a Reply

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