Using PowerCLI to Bulk Add Distributed Port Groups to a Distributed Virtual Switch

By | 9 May 2018

I recently had to install a couple of greenfield environments for a customer that has a very complex network layout. 100’s of VLAN‘s on their network that are consumed by vSphere VM Port Groups on Distributed Switches. Luckily the customer has a standard naming and VLAN ID scheme so I was able to create a list of the VLANs and then run the list through PowerCLI to get all the Distributed Port Groups created without much work. Manually creating them would have taken forever and exporting the old config was not something the customer wanted to do since there were a bunch of legacy VLANs in the existing environment that were no longer needed. Here are the PowerCLI commands to create VLAN backed port groups on a DVS. The DVS name is NEW-DVS and the CSV file is located at c:\networks.csv.

$csv = Import-Csv c:\networks.csv -Header @("name","vlan")
foreach ($rec in $csv) {
   Get-VDSwitch -Name "NEW-DVS" | New-VDPortgroup -Name $rec.name -VLanId $rec.vlan
}

Here is a sample .csv contents as well. This will create 5 port groups on VLAN ID’s 2001-2005:

DPG2001, 2001
DPG2002, 2002
DPG2003, 2003
DPG2004, 2004
DPG2005, 2005

One thought on “Using PowerCLI to Bulk Add Distributed Port Groups to a Distributed Virtual Switch

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.