My notes and ramblings, normally about automation

PowerCLI - Copying PortGroups from a Standard vSwitch to a Distributed vSwitch

· Read in about 1 min · (197 Words)
DvS portgroup PowerCLI PowerShell VMware vSphere VSS

Ran out of ports on a Standard vSwitch and could not reboot the host. Sounds like a good time to implement a Distributed vSwitch and copy the PortGroups over.

When you run the script, it will ask for the desired ESXi host name, desired Standard vSwitch name, and the desired Distributed vSwitch name so you don’t have to modify the code.

#Take port groups from a specified vSwitch on a specified host and create all the same port groups on a specified Distributed vSwitch
$InputHost = Read-Host "Host Name"
$InputvSwitch = Read-Host "vSwitch Name"
$InputDvS = Read-Host "Distributed vSwitch Name"
$vmhost = Get-VMHost $InputHost
$vss = Get-VirtualSwitch -Name $InputvSwitch
$vssNumPorts = $vss.NumPorts
$standardpg = $vmhost | $vss | Get-VirtualPortGroup
$dvs = Get-VDSwitch $InputDvS

foreach ($i in $standardpg) {
    $pvgname = $
    $pvg = "dv-" + $pvgname
    $vlan = $i.VLANID

    #create a Static DvS PG with the same VLAN, number of ports and add a "dv-" to the name
    Get-VDSwitch -Name $dvs | New-VDPortGroup -Name $pvg -VLanId $vlan -PortBinding "Static" -NumPorts $vssNumPorts

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.