PowerCLI change network label for all VMs in a folder

0

Just a quick blurb and a useful one liner used to change all the Network labels for all virtual machines that existed in a vCenter folder.  I had a situation in a lab where I imported numerous server VMs from a production network that would be used for development/testing.  In vCenter these were brought in just with the default first network in the list.  This process is cumbersome to do if you are simply manually editing the properties of each VM and changing the network label/port group.  In steps PowerCLI.  Let’s see how to use PowerCLI change network label for all VMs in a folder.

PowerCLI change network label for all VMs in a folder

To do this we want to first connect into vCenter.  If you connect to an individual host within your environment, using the connect-viserver commandlet, you will not be able to target “folders” in your -location parameter as only in vCenter do you have visibility into folders.  So first let’s connect to vCenter:

connect-viserver yourvcenter.somedomain.com

After we have connected to vCenter, let’s use the one liner we mentioned about to pull out the networkname of the VMs in a particular folder and then set the network adapter network name to a networkname of our choosing.

get-vm -location "MYFOLDER" | get-networkadapter | where {$_.NetworkName -eq "Some Network"} | Set-NetworkAdapter -NetworkName "AnotherNetwork" -confirm:$true

As you can see above, it reads the VMs in the folder location, gets the current network adapter properties – in particular the networkname and then sets that networkname to the network that we want it to be.  I placed a -confirm:$true on the end just to make sure things were setting as expected.

networkname01 PowerCLI change network label for all VMs in a folder

You can easily take the confirmation off the PowerCLI one liner and it will blast through the VMs in the blink of an eye.

Final Thoughts

There are so many PowerCLI network one liners and other scripts out there that make your life as a virtualization admin so much easier.  Mundane tasks such as used to change network label for all VMs in a folder can easily be done with PowerCLI.