vrandom yet another random IT blog

clone vss to dvs


Whilst building out a new environment I needed a really quick script to populate a Distributed Virtual Switch (dvs), mirroring the Port Group configuration of a Standard Switch (vss).


  • Working vSphere Environment
  • vSphere Standard Switch with Port Groups defined
  • vSphere Distributed Switch

The How

Change the variables as appropriate in the script below:

  • “vcsa01.lab.vrandom.com” - the vCenter which manages the environment
  • “hyp-c01.lab.vrandom.com” - the Hypervisor who’s network we will copy
  • “vSwitch1” - the ‘source’ vSphere Standard Switch - we’re going to copy the Port Groups from this
  • “dvSwitch1” - the ‘destination’ vSphere Distributed Switch - we’re going to create port groups on this
Connect-VIServer -Server "vcsa01.lab.vrandom.com"

$vss = Get-VirtualSwitch -VMHost "hyp-c01.lab.vrandom.com" -Name "vSwitch1"
$standardpg = Get-VirtualPortGroup -Standard -VirtualSwitch $vss
$dvs = Get-VDSwitch -Name "dvSwitch1"

foreach ($i in $standardpg) {
	$pvgname = $i.name.ToString()
	$pvg = "dv-" + $pvgname
	$vlan = $i.VLANID
	New-VDPortGroup -Name $pvg -VDSwitch $dvs -VLanId $vlan -PortBinding "Static"

You can change or add additional options into the “New-VDPortGroup” statement, if you need to customise the dvs portgroup that is created. The reason for specifying a particular hypervisor to pull the source vSwitches from is that Standard vSwitches are defined on each hyp, and so the script will generate errors as it tries to create duplicate distributed port groups with the same name if we don’t do this.


The script was originally inspired by this post, and updated to work with the latest PowerCLI releases.