{"id":3101,"date":"2018-04-25T19:53:04","date_gmt":"2018-04-25T23:53:04","guid":{"rendered":"https:\/\/thebackroomtech.com\/?p=3101"},"modified":"2020-01-06T14:06:59","modified_gmt":"2020-01-06T19:06:59","slug":"vmware-powercli-create-port-groups-script","status":"publish","type":"post","link":"https:\/\/thebackroomtech.com\/vmware-powercli-create-port-groups-script\/","title":{"rendered":"VMware PowerCLI &#8211; Create Port Groups Script"},"content":{"rendered":"<p>Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task.<\/p>\n<p>That\u2019s why I came up with this script that will make our life easier and will add the new VLAN\/Network\/PortGroup to all ESXi hosts that are part of a specific cluster.<\/p>\n<pre> Add-PSSnapin VMware.VimAutomation.Core\r\n #Variables\r\n $viserver = \"vcenter.lab.local\"\r\n $cluster = \"Production\"\r\n $vSwitch = \"vSwitch0\"\r\n $PGName = \"VMXNET-VLAN-200\"\r\n $PGVLANID = \"200\"\r\n\r\n Connect-VIserver $viserver -User \"user\" -Password \"password\"\r\n $vmhosts = Get-Cluster $cluster | Get-VMhost\r\n\r\n ForEach ($vmhost in $vmhosts)\r\n {\r\n Get-VirtualSwitch -VMhost $vmhost -Name $vSwitch | New-VirtualPortGroup -Name $PGName -VlanId $PGVLANID\r\n }\r\n<\/pre>\n<p><a href=\"https:\/\/thebackroomtech.com\/wp-content\/uploads\/create-port-groups-vmware.jpg\"><img decoding=\"async\" class=\"size-full wp-image-3102 aligncenter\" src=\"https:\/\/thebackroomtech.com\/wp-content\/uploads\/create-port-groups-vmware.jpg\" alt=\"\" width=\"509\" height=\"266\" srcset=\"https:\/\/thebackroomtech.com\/wp-content\/uploads\/create-port-groups-vmware.jpg 509w, https:\/\/thebackroomtech.com\/wp-content\/uploads\/create-port-groups-vmware-300x157.jpg 300w, https:\/\/thebackroomtech.com\/wp-content\/uploads\/create-port-groups-vmware-80x42.jpg 80w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/a><\/p>\n<p>Before we run this script and enjoy work being done for us for a change, let\u2019s dissect it and see what every line does.<\/p>\n<p><strong>Add-PSSnapin VMware.VimAutomation.Core<\/strong> \u2013 adds the PowerCLI SnapIn to the PowerShell session so we can take advantage of all the CLI commands.<\/p>\n<p><strong>$viserver = &#8220;vcenter.local&#8221;<\/strong> \u2013 specifies the vCenter server we want to connect to<br \/>\n<strong>$cluster = &#8220;Production&#8221;<\/strong> \u2013 specifies the cluster that all the ESXi hosts are a part of<br \/>\n<strong>$vSwitch = &#8220;vSwitch0&#8221;<\/strong> \u2013 specifies the vSwitch we want to add the new VLANs (Networks\/PortGroups) to<br \/>\n<strong>$PGName = &#8220;VMXNET-VLAN-200&#8221;<\/strong> \u2013 here we specify the name of the new VLAN\/Network\/PortGroup<br \/>\n<strong>$PGVLANID = &#8220;200&#8221;<\/strong> \u2013 and here is where we put in the VLAN ID (most important component)<\/p>\n<p>Now that all our variables are in place, let\u2019s start creating the new Port Groups.<\/p>\n<p><strong>Connect-VIserver $viserver -User &#8220;lab.local\\admin&#8221; -Password &#8220;1234567&#8221;<\/strong> \u2013 On this line we connect to the vCenter server and we supply the username and password so that we won\u2019t get any prompts.<\/p>\n<p><strong>$vmhosts = Get-Cluster $cluster | Get-VMhost<\/strong> \u2013 Here we are getting all the cluster resources and, more specifically, retrieving all the ESXis and outputting them as an array to a variable called <strong>$vmhosts<\/strong>.<\/p>\n<p>Now that we have all our info retrieved, it\u2019s time to actually create the Port Groups:<\/p>\n<pre>ForEach ($vmhost in $vmhosts)\r\n {\r\n      Get-VirtualSwitch -VMhost $vmhost -Name $vSwitch | New-VirtualPortGroup -Name $PGName -VlanId $PGVLANID\r\n }<\/pre>\n<p>Here we go into a <strong>ForEach<\/strong> loop. For each element in the $vmhosts array, we run the<strong> New-VirtualPortGroup<\/strong> command with the variables we declared in the first section.<\/p>\n<p>That\u2019s it! Simple isn\u2019t it? Thanks for taking the time to read this tutorial, and hope it helps everybody out there have a more productive day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":3102,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"updated_on":"","footnotes":""},"categories":[56],"tags":[],"class_list":{"0":"post-3101","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-virtualization"},"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO Pro 4.9.5.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task. That\u2019s why I came up with this script that will make our life easier and\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sabrin Alexander\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/thebackroomtech.com\/vmware-powercli-create-port-groups-script\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO Pro (AIOSEO) 4.9.5.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"The Back Room Tech - Serving up the info for back room techs everywhere\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"VMware PowerCLI \u2013 Create Port Groups Script - The Back Room Tech\" \/>\n\t\t<meta property=\"og:description\" content=\"Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task. That\u2019s why I came up with this script that will make our life easier and\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/thebackroomtech.com\/vmware-powercli-create-port-groups-script\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2018-04-25T23:53:04+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-01-06T19:06:59+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"VMware PowerCLI \u2013 Create Port Groups Script - The Back Room Tech\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task. That\u2019s why I came up with this script that will make our life easier and\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[]}\n\t\t<\/script>\n\t\t<!-- All in One SEO Pro -->\r\n\t\t<title>VMware PowerCLI \u2013 Create Port Groups Script - The Back Room Tech<\/title>\n\n","aioseo_head_json":{"title":"VMware PowerCLI \u2013 Create Port Groups Script - The Back Room Tech","description":"Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task. That\u2019s why I came up with this script that will make our life easier and","canonical_url":"https:\/\/thebackroomtech.com\/vmware-powercli-create-port-groups-script\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[]},"og:locale":"en_US","og:site_name":"The Back Room Tech - Serving up the info for back room techs everywhere","og:type":"article","og:title":"VMware PowerCLI \u2013 Create Port Groups Script - The Back Room Tech","og:description":"Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task. That\u2019s why I came up with this script that will make our life easier and","og:url":"https:\/\/thebackroomtech.com\/vmware-powercli-create-port-groups-script\/","article:published_time":"2018-04-25T23:53:04+00:00","article:modified_time":"2020-01-06T19:06:59+00:00","twitter:card":"summary_large_image","twitter:title":"VMware PowerCLI \u2013 Create Port Groups Script - The Back Room Tech","twitter:description":"Hello and welcome to all VMware admins. For the few of us who have an enterprise scale VMware environment with hundreds of VLANs and servers, setting up a new VLAN on all our ESXi hosts can be a daunting task. That\u2019s why I came up with this script that will make our life easier and"},"aioseo_meta_data":{"post_id":"3101","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":"2026-03-27 20:31:21","breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"open_ai":null,"ai":null,"created":"2026-03-27 20:27:05","updated":"2026-03-27 21:43:07"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t<a href=\"https:\/\/thebackroomtech.com\" title=\"Home\">Home<\/a>\n<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t<a href=\"https:\/\/thebackroomtech.com\/category\/virtualization\/\" title=\"Virtualization\">Virtualization<\/a>\n<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\tVMware PowerCLI \u2013 Create Port Groups Script\n<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/thebackroomtech.com"},{"label":"Virtualization","link":"https:\/\/thebackroomtech.com\/category\/virtualization\/"},{"label":"VMware PowerCLI &#8211; Create Port Groups Script","link":"https:\/\/thebackroomtech.com\/vmware-powercli-create-port-groups-script\/"}],"_links":{"self":[{"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/posts\/3101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/comments?post=3101"}],"version-history":[{"count":0,"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/posts\/3101\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/media\/3102"}],"wp:attachment":[{"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/media?parent=3101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/categories?post=3101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thebackroomtech.com\/wp-json\/wp\/v2\/tags?post=3101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}