← script library

Automate VM Power Operations on VMware ESXi with PowerShell

VMware & vSphereJanuary 6, 2025

In this post, we are excited to share a powerful PowerShell script that allows you to automate the power operations of your virtual machines (VMs) on VMware ESXi. This script is a handy tool for system administrators looking to efficiently manage multiple VMs by powering them on or off based on your requirements.

powershell
# Load VMware PowerCLI module
Import-Module VMware.PowerCLI -ErrorAction Stop

# Define connection parameters
$esxiHost = "<ESXi_Host>"
$username = "<Your_Username>"
$password = "<Your_Password>"
# Convert the password to a secure string
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
# Connect to the ESXi server
Connect-VIServer -Server $esxiHost -Credential $credentials -ErrorAction Stop

# Define the action
$action = "Start-VM"
# Retrieve all VMs and perform the action
$vmList = Get-VM
foreach ($vm in $vmList) {
    if ($vm.PowerState -eq "PoweredOff" -and $action -eq "Start-VM") {
        Start-VM -VM $vm -Confirm:$false
        Write-Host "Powered on VM: $($vm.Name)"
    }
}

# Disconnect from the ESXi server
Disconnect-VIServer -Server $esxiHost -Confirm:$false

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.