PowerCLI to Migrate VMs across vCenter servers

Below script which was created for a unique requirement with us for migrating our 6.0 vcenter to 6.5 appliance. Since we have planned for complete refresh of the hardware along with vCenter server. we have not used vCenter migration assistance https://blogs.vmware.com/vsphere/2017/01/vcenter-server-appliance-6-5-migration-walkthrough.html

We have two vCenter environment with different set of hardware, we had around 25 hosts on each cluster segregated based on Operating  system and Database Layer. To make the migration easy, datastore zoned to old environment zoned to new environment as well.


  1. AD integration of both vCenter, with access to vCenter servers. (We used same user at both vCenter Servers)
  2. Old and New VMware ESX host associated with same storage
  3. Both environment configured with same set of portgroup (name can be different, I used VLAN tag to identify the portroup)
  4. Install latest version of PowerCLI which enable the vMotion between vCenter servers

# ————————————- Start Script ———————————————–

#                                    Created By Vipin Vasudevan

#                                    Created on 10/20/2017


# Import the powercli modules

Import-Module VMware.VimAutomation.Core

Import-Module VMware.VimAutomation.Common

Import-Module VMware.VimAutomation.Vds

Import-Module VMware.VimAutomation.Storage

# Enter the credential to connect vCenter servers, if you have two user create another $cred variable

$credential = get-credential -Message “Credential to connect Vcenter server eg:domain\user”

$sourcevCenter = Connect-VIServer -Server oldvcentet.domain.com -credential $credential

$destvCenter = Connect-VIServer -Server newvcentet.domain.com -credential $credential

$VMcluster = Read-host “Enter VM cluster name, eg PRD1WIN or PRD1LIN”

# here we opted to select VMs individually, hence we used “Do.. until” you can also select the VM to query using “get-vm” with a filter and use “foreach” loop to run agianst each VMs



$vmname = Read-host “Enter VM name to migrate”

$vm = Get-VM $vmname -Server $sourcevCenter

$sourceDS = Get-Datastore -VM $vm  | Select-Object –First 1

$networkAdapter = Get-NetworkAdapter -VM $vm -Server $sourcevCenter

$sourcenetname = $networkAdapter.NetworkName

$SourcePortGroup = Get-VDPortgroup -Name $sourcenetname -server $sourcevCenter

$Sourcevlan = $SourcePortGroup.vlanconfiguration

$destinationPortGroup = Get-VDPortgroup | ?{$_.vlanconfiguration -like $Sourcevlan}

$destination = Get-Cluster –Name $VMcluster –Server $destvCenter | Get-VMHost | Select-Object –First 1

$destinationDatastore = Get-Datastore $sourceDS -Server $destvCenter

Move-VM -VM $vm -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore

Write-Host “Do you want to Continue migrate new VMs? ” -ForegroundColor green  -NoNewline

Write-Host ” ‘Y’ for Yes and ‘N’ for No” -ForegroundColor Yellow  -NoNewline

$continueee = Read-Host “:”

$vm = @()


until ($continueee -eq “N”)


# ————————————- End Script ———————————————–

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s