Managing the power state of multiple virtual machines in a VMware vSphere environment can be tedious. This PowerShell script facilitates bulk power management by allowing you to start or stop multiple VMs at once. It connects to the vCenter server, retrieves all VMs matching a specified name pattern, and then either powers them on or off, based on your preference.
powershell
# Define vCenter server credentials
$vCenterServer = "vCenter_Server"
$User = "your_username"
$Password = "your_password"
# Connect to vCenter
Connect-VIServer -Server $vCenterServer -User $User -Password $Password
# Define the name pattern for the VMs to manage
$namePattern = "TestVM*"
# Retrieve the VMs that match the name pattern
$vms = Get-VM -Name $namePattern
# Choose action: "Start" to power on or "Stop" to power off the VMs
$action = "Start" # Change to "Stop" to shut down VMs
# Execute the chosen action on retrieved VMs
foreach ($vm in $vms) {
if ($action -eq "Start") {
Start-VM -VM $vm
Write-Host "Powered on VM: $($vm.Name)"
} elseif ($action -eq "Stop") {
Stop-VM -VM $vm -Confirm:$false
Write-Host "Powered off VM: $($vm.Name)"
}
}
# Disconnect from vCenter server
Disconnect-VIServer -Server $vCenterServer -Confirm:$falseRun 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.
More in VMware & vSphere
Ready when you are.
Try ServerEngine free for 7 days.