Master your VMware vSphere environment with these essential PowerCLI scripts. They simplify daily management, monitoring, and troubleshooting tasks, boosting your efficiency significantly.
1. List All VMs with Power State and Host
Get a quick overview of all virtual machines, their power state, and running host.
Get-VM | Select-Object Name, PowerState, @{Name='Host';Expression={$_.VMHost.Name}} | Format-Table -AutoSize
2. Check VM Tools Status for All VMs
Verify VMware Tools installation and running status across your VMs.
Get-VM | Select-Object Name, @{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}} | Format-Table -AutoSize
3. Get Datastore Usage Summary
Monitor datastore free space and capacity percentages to anticipate storage needs.
Get-Datastore | Select-Object Name, FreeSpaceGB, CapacityGB, @{N="FreePercent";E={[math]::Round(($_.FreeSpaceGB/$_.CapacityGB)*100,2)}} | Format-Table -AutoSize
4. Find Snapshots Older Than 30 Days
Identify old snapshots that may impact performance and storage. Cleanup is recommended.
Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-30)} | Select-Object VM, Name, Created | Format-Table -AutoSize
5. Get List of Hosts with CPU and Memory Usage
Track resource utilization on ESXi hosts for capacity planning.
Get-VMHost | Select-Object Name, @{N="CPU_Usage(%)";E={[math]::Round(($_.CpuUsageMHz / $_.CpuTotalMHz)*100,2)}}, @{N="Memory_Usage(%)";E={[math]::Round(($_.MemoryUsageMB / $_.MemoryTotalMB)*100,2)}} | Format-Table -AutoSize
6. VMs with High CPU Usage in Last Hour
List VMs consuming more than 80% CPU average to spot bottlenecks.
$oneHourAgo = (Get-Date).AddHours(-1)
Get-Stat -Entity (Get-VM) -Stat cpu.usage.average -Start $oneHourAgo |
Group-Object -Property Entity | ForEach-Object {
[PSCustomObject]@{
VMName = $_.Name
AvgCPU = ($_.Group | Measure-Object -Property Value -Average).Average
}
} | Where-Object { $_.AvgCPU -gt 80 } | Sort-Object -Property AvgCPU -Descending | Format-Table -AutoSize
7. Power Off All VMs on a Specific Host
Useful for host maintenance or shutdown, ensuring controlled VM power-off.
$host = "esxi-hostname"
Get-VM -VMHost $host | Where-Object {$_.PowerState -eq "PoweredOn"} | Stop-VM -Confirm:$false
8. Create a New VM Folder and Move Specified VMs
Organize virtual machines into folders programmatically for better vCenter management.
$folderName = "NewFolder"
$vmNames = @("VM1", "VM2", "VM3")
$folder = Get-Folder -Name $folderName -ErrorAction SilentlyContinue
if (-not $folder) { $folder = New-Folder -Name $folderName -Location (Get-Datacenter) }
foreach ($vmName in $vmNames) {
Get-VM -Name $vmName | Move-VM -Destination $folder
}
9. Export VM List to CSV with Key Info
Generate reports for auditing, capacity planning, or inventory by exporting VM details.
Get-VM | Select-Object Name, PowerState, NumCPU, MemoryGB, ProvisionedSpaceGB | Export-Csv -Path "C:\VMReport.csv" -NoTypeInformation
10. Check VM Network Adapters and IP Addresses
Assess VM connectivity, IP addresses, and network adapter configurations.
Get-VM | Select-Object Name, @{N="IPAddresses";E={$_.Guest.IPAddress -join ","}}, @{N="NetworkAdapters";E={$_.NetworkAdapters.Name -join ","}} | Format-Table -AutoSize
How to Use
First, install PowerCLI: Install-Module VMware.PowerCLI. Then, connect to your vCenter or ESXi host: Connect-VIServer -Server your_vcenter_server. Copy and run the scripts in your PowerShell console. Always test in a non-production environment first.
These PowerCLI scripts are fundamental tools for any VMware administrator. Utilize them to enhance your operational efficiency and maintain a robust virtualized infrastructure. Happy scripting!