Bulk VM Power Management in vSphere Using PowerShell

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.
### Step 1: Connect to vCenter Server
First, you need to connect to your vCenter server. Replace the placeholders with your own credentials and vCenter details.
“`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

“`
### Step 2: Retrieve VMs by Name Pattern
The next step is to define a name pattern for the virtual machines you wish to manage. This will allow you to filter which VMs to power on or off.
“`powershell

# Define the name pattern for the VMs to manage
$namePattern = "TestVM*"
# Retrieve the VMs that match the name pattern
$vms = Get-VM -Name $namePattern

“`
### Step 3: Power On or Off the VMs
Now, you can power on or off the VMs retrieved in the previous step. You can change the action to either Start-VM or Stop-VM based on your requirement.
“`powershell

# 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)"
    }
}

“`
### Step 4: Disconnect from vCenter
Finally, its essential to disconnect from the vCenter server to maintain a clean and efficient session.
“`powershell

# Disconnect from vCenter server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false

“`
By using this script, you can easily manage the power state of multiple VMs, streamlining your operations. For even greater efficiency in server management, check out ServerEngine at [https://serverengine.co](https://serverengine.co). Our software is designed to optimize your IT environment and enhance productivity.