← script library

Power Off a VMware VM with PowerCLI

VMware & vSphereAugust 17, 2026

The same PowerCLI connect-act-disconnect shape for the other direction: look the VM up by name, report clearly when it is already powered off, and otherwise stop it and log the outcome. Every failure path writes a log line with the exception message rather than failing silently.

powershell
# Set target VM name

$server = "esxi.example.local"

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true -Confirm:$false

$username = "SE-CredentialsStore.Username.(esxi.example.local)"
$password = "SE-CredentialsStore.Password.(esxi.example.local)"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $securePassword)

Connect-VIServer -Server $server -Credential $credential

$vm = Get-VM -Name "$vmName" -ErrorAction SilentlyContinue

if (-not $vm) {
    Write-Host "<WRITE-LOG = ""*ERROR: VM not found: $vmName*"">"
} elseif ($vm.PowerState -eq "PoweredOff") {
    Write-Host "<WRITE-LOG = ""*INFO: VM $vmName is already powered off.*"">"
} else {
    try {
        Stop-VM -VM $vm -Confirm:$false | Out-Null
        Write-Host "<WRITE-LOG = ""*SUCCESS: VM $vmName powered off.*"">"
    } catch {
        Write-Host "<WRITE-LOG = ""*ERROR: Failed to power off VM $vmName - $($_.Exception.Message)*"">"
    }
}

Disconnect-VIServer -Server $server -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.