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.


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


11 thoughts on “PowerCLI: Mass VM Portgroup Change

  1. Vignesh Sivasubramaniyan

    foreach($vmpg in Import-Csv -Path C:\temp\VMPortGroups.csv -UseCulture){

    Get-VM -Name $vmpg.VM | Get-NetworkAdapter -Name $vmpg.NIC |

    Set-NetworkAdapter -Portgroup $vmpg.Network -Confirm:$false

    pls advise is it work

  2. Erik B. Grimsrud

    I know this is a old post, but it helped me put together a script to change the port group for multiple NICs on a 100,000+ VMs.
    NOTE: As a FYI. In 6.0 and 6.5 there is a bug that causes the “Direct Path I/O” in the NIC properties to be enabled after the port group change.

  3. Lakshminarayana Kandula

    Hi Alan,
    Request you to provide a script to add a vNIC to multiple VMs with same portgroup.
    Thanks in advance.

  4. Mukesh Nakade

    @Faaris – How did you given the old and new port group name in same .csv file.

    Can i get the format of PGMigration.csv file

  5. Mukesh Nakade

    How did you given the old and new port group name in same .csv file.

    Can i get the format of PGMigration.csv file

  6. Faaris

    I would like to import a csv with defined Old Port Groups and New Port Groups broken by line. So far, I have not gotten to function. Here’s a sample of what I’m trying to use:

    #### Variables
    $VC = Read-Host “Enter Your vCenter Server”
    $Cluster = Read-Host “Enter Your Cluster Name”
    $VFILE = Import-Csv c:\pcli\PGMigration.csv

    #### Connect to Your vCenter
    Connect-viserver $VC

    #### Execution Area

    $VFILE | % {

    $OLDPG = $_.OldPG
    $NEWPG = $_.NewPG
    Get-Cluster $Cluster | Get-VM | Get-NetworkAdapter | Where {$_.NetworkName -eq $OLDPG } | Set-NetworkAdapter -NetworkName $NEWPG -Confirm:$false


    #### Disconnect from Your vCenter
    Disconnect-VIServer $VC -Confirm:$false

  7. bobby

    Hi there,

    VESI’s site seems to be redirecting to [http://www.powergui.org/index.jspa]

    Is it the same software?

    btw, love your scripts!!!!!!!

  8. Woodpecker

    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?

  9. Pingback: uberVU - social comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.