PowerCLI one-liner to rename a PortGroup on all ESX/ESXi hosts in a cluster

It can be a huge hassle to manually rename PortGroups across multiple ESX/ESXi hosts in a cluster. This is where PowerCLI becomes super handy – the one-liner below will take of it:

Get-Datacenter London-DC | Get-Cluster "ESX Cluster" | Get-VMHost | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN" -vlanid 1002

The above command connects into the London-DC Datacenter object and then to the Cluster ESX Cluster. It will get all the ESX/ESXi servers in the cluster by running Get-VMHost. It then locates the PortGroup called Inside-VLAN and renames it to DMZ-VLAN and tags it with vlan ID 1002.

If you have upwards of 5 ESX/ESXi hosts it will save you a lot of time and effort.

PowerCLI one-liner to rename a PortGroup on an ESX/ESXi host

If having to login to your VMware vSphere infrastructure is a hassle just to rename a PortGroup on a single ESX/ESXi host is a hassle you can run the following PowerCLI one-liner to take care of business:

Get-VMHost -name esx1 | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN"

The command connects to the ESX/ESXi host called esx1 and renames the PortGroup called Inside-VLAN to DMZ-VLAN.

If the PortGroup being changed is the native vlan on a trunk port then you would not tag it. However, if it was tagged with a vlan id and you were changing your network configuration this could also mean that you want to change the vland id. If so add the -vlanid parameter followed by the id:

Get-VMHost -name esx1 | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN" -vlanid 1002

Simple as that!