Copy vSwitch portgroups to a new host

Based on: PowerCLI: Easy vSwitch & PortGroup Setup | Virtu-Al.Net.

Here’s a great one-liner to copy all the portgroups from one host’s vswitch to a new host’s vswitch (example below copies oldhost’s portgroups on vSwitch1 to newhost’s portgroups on vSwitch2, keeping VLAN IDs intact):


$oldhost = get-vmhost -name oldhost.local.dom
$newhost = get-vmhost -name newhost.local.dom

$oldhost | get-virtualswitch -name vSwitch1 | get-virtualportgroup | foreach { $newportgroup = $newhost | get-virtualswitch -name vSwitch2 | new-virtualportgroup -name $_.Name -VLanID $_.VLanID }