VMware ESXi – PowerCLI for bulk vSwitch changes

The reward for effort when scripting is almost always worth it. Today I had the task of removing a particular AntiVirus product from a 36 host VDI cluster. Part of this requires the removal of NSX-Manager and all the groovy components it uses to do it’s thing, including a vSwitch on every host, with multiple port-groups.


I instantly knew this would take significant time if I went down the click-click-click track for all 36 hosts. Here’s how I went about it using PowerCLI.

First step is to identify the name of the vwsitch in question

Get-vSwitch shows all the vSwitches in the connected VI server’s inventory. Here it is, “vmservice-vswitch

Let’s get a list of all the hosts with this vswitch

We need to kind of work backwards here;

  • 1-  Remove the VMkernel ports
  • 2- Remove the Virtual Port Groups
  • 3- Remove the Virtual switches.

The removal order is required as you can’t delete these objects while they contain other objects,

Let’s get a list of the applicable vmkernel ports;

Looks consistent.

Next, remove the Virtual Port Group.

Nervous ? use the -Whatif switch to do a sanity check. Looks right.

Check they’re gone;


Where now good to go. Let’s remove all the vSwitches.

Gone ? You bet.

Checking back in the UI, there’s a raft of completed tasks.

Of course you can check using Get-Tasks also.


Do yourself a favour and have things like this in your “cookbook” so you can save yourself time not having to do repetitive, mundane tasks.

Over and out.










Related Post

Leave a Reply

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

%d bloggers like this: