← script library

Automating VM Reports in VMware vSphere with PowerShell

VMware & vSphereJanuary 6, 2025

Generating reports for virtual machines in VMware vSphere can be a tedious task. This PowerShell script automates the creation of reports that summarize key VM information, such as name, power state, and resource allocation. By using this script, you can quickly produce reports to assist in managing your virtual environment effectively.

powershell
# Load VMware PowerCLI module
Import-Module VMware.PowerCLI

# Connect to the vSphere server
$vCenterServer = "your_vcenter_server"
$username = "your_username"
$password = "your_password"
Connect-VIServer -Server $vCenterServer -User $username -Password $password

# Define the VMs to include in the report
$vmNames = @("VM1", "VM2", "VM3") # Replace with your VM names

# Generate the VM report
$report = @()
foreach ($vmName in $vmNames) {
    $vm = Get-VM -Name $vmName
    if ($null -ne $vm) {
        $reportEntry = [PSCustomObject]@{
            Name       = $vm.Name
            PowerState = $vm.PowerState
            CPUCount   = $vm.NumCPU
            MemoryGB   = [math]::round($vm.MemoryGB, 2)
        }
        $report += $reportEntry
    } else {
        Write-Host "VM $vmName not found."
    }
}
# Output the report to the console
$report | Format-Table -AutoSize

# Export the report to a CSV file
$report | Export-Csv -Path "C:\Path\To\Your\Report.csv" -NoTypeInformation
Write-Host "Report exported to C:\Path\To\Your\Report.csv"

# Disconnect from the vSphere server
Disconnect-VIServer -Server $vCenterServer -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.