PowerCLI command to list all PortGroups on an ESX/ESXi host

A Lead Engineer asked me to send him a list of all PortGroups on a specific ESX host (running ESX 4.1). Due to the time and effort required to obtain this info manually, I had to use PowerCLI. I ran the command below:

Get-VirtualPortGroup -VMHost esx1 | select Name, VirtualSwitch, VLanId

It outputs results similar to the below:

Name                     VirtualSwitch      VLanId
----                     -------------      ------
DMZ-VLAN                 vSwitch0           1139
Inside-VLAN              vSwitch0           805
VMNet                    vSwitch1           0
VMNet-KS                 vSwitch1           124
VMNet-SC                 vSwitch1           0
VMNet-vmk                vSwitch1           0
Backup-Inside-VLAN       vSwitch2           648
Backup-DMZ-VLAN          vSwitch2           647

To get the results in a spreadsheet export the results to a csv file per the below:

Get-VirtualPortGroup -VMHost esx1 | select Name, VirtualSwitch, VLanId | | Export-Csv C:\VMHostNetworkInfo.csv

The full command line reference for Get-VirtualPortGroup is available here.

PowerCLI one-liner to change PortGroup membership on all VMs in a cluster

In my previous post I provided a PowerCLI command to rename PortGroups across multiple hosts in an ESX/ESXi cluster. You would usually do this when making network segment/infrastructure changes. These would be done in two phases:

  1. Rename PortGroups and tag them with the new vlan(s).
  2. Move VMs from the old network segment (PortGroups) into the new one.

The following command would do it:

Get-Cluster "ESX Cluster" | Get-VM |Get-NetworkAdapter | Where {$_.NetworkName -eq "Inside-VLAN" } |Set-NetworkAdapter -NetworkName "DMZ-VLAN" -Confirm:$false

So the command connects to all the VMs running in Cluster ESX Cluster and changes the networks or PortGroups the vNICs are located in from Inside-VLAN to DMZ-VLAN. The last parameter -Confirm:$false suppresses the prompt to confirm the PortGroup changes.