There are probably better ways to do this, but I found this useful, and it’s likely to be something that I’ll have to do again, so I’m writing it down here. Maybe you can use it, too…

$sourceVS = get-virtualswitch -vmhost SOURCE-HOST -name vSWITCH-NAME

$targetVS = get-virtualswitch -vmhost TARGET-HOST -name vSWITCH-NAME

$sourceVS | get-VirtualPortGroup | ForEach-Object {

New-VirtualPortGroup -name $_.Name -VirtualSwitch $targetVS -VLanId $_.VLanId

}

Just make sure to change SOURCE-HOST, TARGET-HOST, and vSWITCH-NAME to the right strings and you’re good to go. If you want to do a sanity check, add a Write-Host in front of the New-VirtualPortGroup command, and it’ll print out the lines that it would have executed.

If you’re from a Linux background like I am, you probably have a tendency to freak out because there aren’t quotes around variables (particularly the $_.Name variable), but it’s an object, and as in so many other cases, PowerShell manages to do the right thing.