Changing vNIC Port Group Assignment in VMware with PowerShell

Experienced PowerShell users won’t find this post very helpful, but less experienced PowerShell users—or even PowerShell newbies such as me—may find this handy. Today I had a need to change the port group assignment on the vNICs for a bunch of guest VMs in the lab. Rather than manually click through all these VMs just to change the port group, I decided to give PowerShell a try.

Thanks to this post by Cody Bunch and this Twitter response by Hal Rottenberg, I cobbled together this PowerShell command:

get-datacenter "Name" | get-vm | get-networkadapter | where-object { $_.networkname -like "OldPortGroup" } | set-networkadapter -networkname "NewPortGroup" -Confirm:$false

It worked like a champ! Obviously, you could limit the scope of this command by filtering the VMs that are returned with a wildcard pattern on the Get-VM command.

Thanks to Cody and Hal for their assistance!

Tags: , , , ,

  1. Don’s avatar

    Scott;

    Could you provide a screen shot of what your port group assignment looked like before and after running this command? I’d like to “see” what it does? Thanks. Don Whittaker

  2. slowe’s avatar

    Don,

    The only thing this did was change the network to which the vNIC was associated for each VM. So, my old VM network was called “Servers-VLAN2″, but I wanted to switch that over to “VLAN2-Servers.” So I created the new VM network and ran this command to change all the VMs from the old network to the new network.