Need help with shutdown and startup scripts

Forgive me, I know I could search for these things but I'm running short on time and need some help from the community.

Here's what I'm trying to do:

  1. Shutdown Script
    1. Before the shutdown script runs, export a list of all powered on VMs to a text file.
    2. Shutdown all powered on VMs
    3. Put all hosts into maintenance mode
    4. Shutdown all hosts.
  2. Startup Script
    1. (All hosts will be manually powered on)
    2. Take all hosts out of maintenance mode.
    3. Power on all VMs found in the list from step 1.1 and confirm any questions.

Any help would be greatly appreciated!

Tags (1)
0 Kudos
1 Reply

Try something like this

$vmsOn = Get-VM | Where {$_.PowerState -eq "poweredon"}

$vmsON | Export-Csv "C:\poweredon-vms.csv" -NoTypeInformation -UseCulture

$vmsOn | Stop-VM -Confirm:$false

$esx = Get-VMHost

$esx | Set-VMHost -State maintenance

$esx | Stop-VMHost


$esx = Get-VMHost

$esx | Set-VMHost -State connected

Import-Csv "C:\poweredon-vms.csv" -UseCulture | %{



If your VMs all have the VMware Tools installed, you can replace the ungracefull Stop-VM by a Shutdown-VMGuest.

Blog:  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos