← script library

Automating VMware vSphere VM Power Operations with PowerShell

VMware & vSphereJanuary 7, 2025

In this script, we will show you how to power on, power off, and check the power state of your virtual machines (VMs) using PowerShell. This can be especially helpful for managing resources in your data center efficiently.

powershell
# Load the VMware PowerCLI module
Import-Module VMware.PowerCLI

# Connect to the vCenter server
$vcServer = "your_vcenter_server"
$credential = Get-Credential
Connect-VIServer -Server $vcServer -Credential $credential

# Get a list of all VMs
$vmList = Get-VM
$vmList | Select-Object Name, PowerState

# Power on a specific VM
$vmToPowerOn = Get-VM -Name "Your_VM_Name"
if ($vmToPowerOn -and $vmToPowerOn.PowerState -ne "PoweredOn") {
    Start-VM -VM $vmToPowerOn
    Write-Host "Started VM:" $vmToPowerOn.Name
} else {
    Write-Host "VM is already powered on or does not exist."
}

# Power off a specific VM
$vmToPowerOff = Get-VM -Name "Your_VM_Name"
if ($vmToPowerOff -and $vmToPowerOff.PowerState -eq "PoweredOn") {
    Stop-VM -VM $vmToPowerOff -Confirm:$false
    Write-Host "Stopped VM:" $vmToPowerOff.Name
} else {
    Write-Host "VM is already powered off or does not exist."
}

# Check the power state of the VM
$vmToCheck = Get-VM -Name "Your_VM_Name"
if ($vmToCheck) {
    Write-Host "The power state of VM" $vmToCheck.Name "is:" $vmToCheck.PowerState
} else {
    Write-Host "VM does not exist."
}

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.