Introduction

In multi-site or enterprise VMware environments, admins often manage more than one vCenter. Manually switching between vCenters slows automation. PowerCLI allows you to connect to multiple vCenters simultaneously and run queries or scripts across all of them.

In this article, you will learn to:

  • Connect to multiple vCenters in one session
  • Run PowerCLI commands against specific vCenters
  • Aggregate and filter cross-vCenter results
  • Export multi-site VM or host reports
  • Avoid common multi-vCenter mistakes

Step 1: Define Your vCenter Servers

$vCenters = @("vcenter-dfw.lab.local", "vcenter-nyc.lab.local", "vcenter-fra.lab.local")
$creds = Get-Credential

Use a loop to connect:

foreach ($vc in $vCenters) {
Connect-VIServer -Server $vc -Credential $creds
}

Confirm all active connections:

Get-VIServer

Step 2: Run Commands Against All vCenters

PowerCLI automatically runs Get-VM, Get-VMHost, and other core cmdlets across all connected vCenters.

Get-VM | Select Name, VMHost, @{N="vCenter";E={$_.Uid.Split("@")[1]}}

This pulls VMs from all vCenters and tags their origin.


Step 3: Filter by vCenter Using -Server

Target a specific vCenter:

Get-VM -Server "vcenter-nyc.lab.local"

Target two vCenters only:

Get-VM -Server ("vcenter-dfw.lab.local", "vcenter-fra.lab.local")

Step 4: Export Cross-vCenter Report

$report = Get-VM | Select Name, PowerState, VMHost, @{N="vCenter";E={$_.Uid.Split("@")[1]}}
$report | Export-Csv "C:ReportsMultiVC_VM_List.csv" -NoTypeInformation

Add conditional filters:

$poweredOff = Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"}
$poweredOff | Export-Csv "C:ReportsAllSites_PoweredOffVMs.csv" -NoTypeInformation

Diagram: Multi-vCenter Workflow with PowerCLI


Step 5: Safely Disconnect from Specific vCenters

Disconnect-VIServer -Server "vcenter-nyc.lab.local" -Confirm:$false

Or disconnect from all:

Disconnect-VIServer * -Confirm:$false

Use Case: Global VM Inventory Script

$vCenters = @("vcenter-01", "vcenter-02", "vcenter-03")
$creds = Get-Credential

foreach ($vc in $vCenters) {
Connect-VIServer -Server $vc -Credential $creds
}

$allVMs = Get-VM | Select Name, VMHost, PowerState, @{N="vCenter";E={$_.Uid.Split("@")[1]}}
$allVMs | Export-Csv "C:ReportsAllSites_VM_Inventory.csv" -NoTypeInformation

Disconnect-VIServer * -Confirm:$false


Troubleshooting

Problem Fix
Session not maintained for multiple vCenters Always use -Server explicitly when working across multiple connections
Disconnect command logs out all connections Use -Server with Disconnect-VIServer to target one at a time
Uid parsing fails for vCenter label Use ExtensionData.Client.ServiceUrl for more precise tagging
Output duplicates from different sites Use Group-Object or Sort-Object -Unique on report outputs

Similar Posts