PowerCLI: Mass VM Portgroup Change
I was asked in my Script List if it was possible to change Multiple VMs to attach to a new PortGroup, this task is easy enough so long as you remember that every VM can have up to 4 virtual nics in VI3 and up to 10 in vSphere.
So to change all virtual machines we would do the following:
$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"
Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false
Or to change all of the VMs for a particular cluster we could use:
$Cluster = "Non-Production"
$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"
Get-Cluster $Cluster |Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false
Note: By default the Set-NetworkAdapter cmdlet would prompt you for each change it was going to make, this is overwritten by using the –Confirm:$false parameter.
VESI
This can also be achieved from the VESI by:
- Connect to a managed host
- Select Virtual Machines
- Select the virtual machines from the grid view which you would like to change
- Click ‘Network Adapters’ from the links pane on the right
- Filter the grid view to only display the adapters attached to a certain Network Name
- Click ‘Change Properties’ from the Actions pane on the right and enter a new NetworkName as seen below







about 4 months ago
Awesome! Totally awesome!! Thanks!@
about 3 weeks ago
What if i had to change only the name of the portgroup, for example from a to b, the minute i change it in the host level, all the vm’s will lose the network, will this script still work? or it will fail to get the old network?