PowerCLI Script To Export vSphere Inventory To CSV
A current vSphere inventory gives infrastructure teams a practical record of virtual machines, ESXi hosts, datastores, clusters, networks, and their operational state. Exporting that information to CSV with PowerCLI is quicker and more consistent than copying details from the vSphere Client, particularly when an environment spans several clusters or vCenter Server instances.
A CSV export can support capacity reviews, migration planning, licence discussions, audit preparation, and troubleshooting. It also creates a lightweight snapshot that can be compared with later exports to identify new workloads, retired hosts, or changes in datastore usage.
Why Export vSphere Inventory
Inventory data is useful during routine administration and larger infrastructure projects. A Melbourne service provider might use it to check customer workloads before a maintenance window, while an internal team in Sydney could use the same report to validate assets before a data-centre refresh.
PowerCLI can collect information directly from vCenter Server, which reduces manual errors and keeps the output repeatable. The report can be opened in Excel, imported into Power BI, committed to Git, or processed by another automation tool.
Prepare The PowerCLI Session
Install a current VMware PowerCLI release on a workstation or management server with access to the relevant vCenter Server. Modern versions run on PowerShell 7 as well as Windows PowerShell, making them suitable for Windows, Linux, and macOS administration hosts.
Connect with an account that has read-only permissions across the inventory. Using a dedicated account is preferable to using a highly privileged administrator account, especially where the exported file may be stored on shared infrastructure or copied between systems.
Export The Inventory To CSV
The following script creates one CSV containing virtual machines, ESXi hosts, datastores, and distributed or standard virtual networks. It records common fields while handling values that are not available for every object type.
# Connect to vCenter Server
$vCenter = "vcsa01.example.com"
$OutputFile = ".\vSphere-Inventory.csv"
Connect-VIServer -Server $vCenter
$Inventory = @()
# Virtual machines
$Inventory += Get-VM | Select-Object `
@{N="ObjectType";E={"VirtualMachine"}},
Name,
@{N="Parent";E={$_.VMHost.Parent.Name}},
@{N="Host";E={$_.VMHost.Name}},
PowerState,
NumCpu,
MemoryGB,
ProvisionedSpaceGB,
UsedSpaceGB,
@{N="Datastore";E={($_.Datastore | Select-Object -ExpandProperty Name) -join "; "}}
# ESXi hosts
$Inventory += Get-VMHost | Select-Object `
@{N="ObjectType";E={"ESXiHost"}},
Name,
@{N="Parent";E={$_.Parent.Name}},
@{N="Host";E={$_.Name}},
ConnectionState,
PowerState,
NumCpu,
MemoryTotalGB,
Version,
Build
# Datastores
$Inventory += Get-Datastore | Select-Object `
@{N="ObjectType";E={"Datastore"}},
Name,
@{N="Parent";E={$_.Parent.Name}},
@{N="Host";E={""}},
State,
CapacityGB,
FreeSpaceGB,
Type
# Networks
$Inventory += Get-VirtualPortGroup | Select-Object `
@{N="ObjectType";E={"Network"}},
Name,
@{N="Parent";E={$_.VirtualSwitch.Name}},
@{N="Host";E={$_.VMHost.Name}},
@{N="VLAN";E={$_.VlanId}}
$Inventory | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Disconnect-VIServer -Server $vCenter -Confirm:$false
Write-Host "Inventory exported to $OutputFile"
The exact properties available can vary between PowerCLI versions and vSphere releases. If a command returns an unexpected null value, test the object interactively with Get-VM vm-name | Format-List * and adjust the selected properties.
Useful Inventory Fields
A practical report should contain enough context to identify an object without creating an unnecessarily large extract. The following fields are usually valuable:
- Object type and object name
- Cluster, folder, or parent location
- Power state or connection state
- CPU, memory, capacity, and free space
- ESXi version, build, datastore, and network
For larger environments, create separate CSV files for virtual machines, hosts, datastores, and networks. A single combined file is convenient for quick reviews, but separate files are easier to filter and analyse when thousands of objects are involved.
Schedule Repeatable Exports
A scheduled PowerShell task can run the script daily or weekly and save each report with a date in its filename. For example, $OutputFile = ".\Inventory-$((Get-Date).ToString('yyyy-MM-dd')).csv" creates a dated snapshot that can be compared over time.
Many Australian organisations perform changes outside normal office hours, including evening maintenance in Brisbane or early-morning work in Perth. Scheduling exports before a change window provides a baseline without adding manual work to the operations runbook.
Validate The Export And Connection
An empty or incomplete CSV often indicates a connection, permissions, or filtering issue rather than a problem with Export-Csv. Confirm that the session is connected to the intended vCenter and that the account can browse every required datacenter, cluster, and folder.
If an ESXi host appears disconnected, review this ESXi connection guide before interpreting the inventory as complete. A disconnected host may still appear in vCenter, but its hardware or runtime values could be unavailable.
Add Operational Checks
Before relying on an export for an audit or capacity decision, perform a few simple checks. These checks help catch stale sessions, unexpected filtering, and formatting problems.
- Confirm the vCenter name and export timestamp
- Check that expected datacentres and clusters are present
- Compare the VM count with the vSphere Client
- Open the CSV and inspect blank or duplicated values
For a recurring report, add error handling with try, catch, and finally. This allows the script to disconnect cleanly and return a useful failure message when DNS, authentication, certificates, or network access prevent a successful export.
Protect And Use The CSV
Inventory exports can reveal hostnames, workload names, internal network labels, storage platforms, and capacity information. Store them with appropriate access controls, avoid emailing them broadly, and remove old snapshots according to the organisation’s retention policy.
Australian businesses should also consider the Privacy Act 1988 and the Australian Privacy Principles when inventory data can be linked to people, customers, or identifiable systems. VMware licensing and commercial records may also be commercially sensitive, so check the site’s disclaimer and your organisation’s own information-handling rules before publishing an extract.
A PowerCLI export becomes much more useful when it is part of a controlled process: authenticate securely, collect predictable fields, validate the result, and retain only what the operational task requires. Save the script in Git, document its required permissions, and run it from a managed administration host to make future reviews faster and more dependable.