← script library
PowerShell Script for Bulk VM Power Management in VMware vSphere
VMware & vSphereJanuary 6, 2025
In this post, I will share a PowerShell script that simplifies the process of managing multiple virtual machines (VMs) in VMware vSphere. This script allows you to bulk start, stop, or restart VMs, making it a handy tool for IT professionals managing vSphere environments. With just a few commands, you can manipulate the power state of your VMs efficiently. Ensure you have VMware PowerCLI installed and your environment set up correctly before running the script.
powershell
# Install VMware PowerCLI module
Install-Module -Name VMware.PowerCLI -AllowClobber -Force
# Connect to vSphere server
$server = "your_vcenter_server"
$username = "username"
$password = "password"
Connect-VIServer -Server $server -User $username -Password $password
# Get a list of all VMs
$vmList = Get-VM
# Define the power action
$action = "Start" # Change this to "Stop" or "Restart" as needed
# Execute the defined power action
foreach ($vm in $vmList) {
switch ($action) {
"Start" {
Start-VM -VM $vm
Write-Host "Started VM: $($vm.Name)"
}
"Stop" {
Stop-VM -VM $vm -Confirm:$false
Write-Host "Stopped VM: $($vm.Name)"
}
"Restart" {
Restart-VM -VM $vm -Confirm:$false
Write-Host "Restarted VM: $($vm.Name)"
}
}
}
# Disconnect from vSphere server
Disconnect-VIServer -Server $server -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.