PowerCLI

 View Only

 move-vm with multiple adapters to a new destination portgroup

kwg66's profile image
kwg66 posted Dec 12, 2025 11:37 AM

This should be easy, but it isn't... uggggg

Simply trying to migrate VMs that have more than 1 adapter to a new cluster with adapters mapped to a new portgroup at the destination.   From what I read in the documentation is that if you only specify a single portgroup as the destination all the adapters would map this destination portgroup.  However, this is not working as documented.  

Simple code (even the example provided in the Move-VM CMDLET powercli webpage doesn't work):

$vm = Get-VM -Name 'test-VM'
$destPG = Get-VirtualPortGroup -Name 'test-portgroup'
$destination = Get-VMHost 'test-esxi-host'

Get-VM -Name $vm | Move-VM -Destination $destination -PortGroup $destPG -RunAsync -Confirm:$True 

If someone knows how to make a migration work that maps the adapters to a new portgroup please help!

Thanks

Ken

ITSavant's profile image
ITSavant

That cmdlet can accept and process an array of vNics, as opposed to individual items, William L. did an article about it years ago. 

You can also use the method on the $VM.ExtensionData (Get-View, really) and create a $spec object with all the nics mapped.

kwg66's profile image
kwg66

I figured this out with the help of AI..  this works fantastic but not set up for having a VM who's disks are spread across multiple datastores, that would take more work but would most likely use the same coding format as mapping multiple network adapters to different portgroups.

Create CSV with these headers:   VMName,SourceNetworkAdapter,DestinationPortgroup,DestinationCluster,DestinationDatastore

# Connect to vCenter
$vCenter = "xxxxxxxxxx"
Connect-VIServer -Server $vCenter

# Import CSV
$csv = Import-Csv "C:\Script\migration_variables.csv"

# Group entries per VM (since multiple rows per VM for multiple NICs)
$vmGroups = $csv | Group-Object VMName

foreach ($vmGroup in $vmGroups) {

    $vmName = $vmGroup.Name
    $vmEntries = $vmGroup.Group

    Write-Host "Processing VM: $vmName" -ForegroundColor Cyan

    # Get VM
    $vm = Get-VM -Name $vmName -ErrorAction Ignore

    # Destination cluster (same for all rows of this VM)
    $destClusterName = $vmEntries[0].DestinationCluster
    $destCluster = Get-Cluster -Name $destClusterName -ErrorAction Ignore

    # Random host selection within cluster
    $destHost = Get-VMHost -Location $destCluster | 
                Where-Object {$_.ConnectionState -eq "Connected"} | 
                Get-Random

    Write-Host "Selected destination host: $($destHost.Name)" -ForegroundColor Yellow

    # Destination datastore
    $destDatastoreName = $vmEntries[0].DestinationDatastore
    $destDatastore = Get-Datastore -Name $destDatastoreName 
    #$destDatastoreName = $vmName.DestinationDatastore
    

    # Build NIC network mapping
    $networkAdapters = @()

    foreach ($entry in $vmEntries) {

        $nic = Get-NetworkAdapter -VM $vm -Name $entry.SourceNetworkAdapter -ErrorAction Ignore
        $destPG = Get-VirtualPortGroup -Name $entry.DestinationPortgroup -VMHost $destHost -ErrorAction Ignore

        $networkAdapters += @{
            NetworkAdapter = $nic
            PortGroup      = $destPG
        }
    }

    try {
        # Perform migration
        Move-VM -VM $vm `
                -Destination $destHost `
                -Datastore $destDatastore `
                -NetworkAdapter ($networkAdapters.NetworkAdapter) `
                -PortGroup ($networkAdapters.PortGroup) `
                -ErrorAction Ignore

        Write-Host "Migration completed for $vmName" -ForegroundColor Green
    }
    catch {
        Write-Host "Migration failed for $vmName : $_" -ForegroundColor Red
    }
}