Easy PowerCLI script to help migrate VMs from one network to the other.

Home / Easy PowerCLI script to help migrate VMs from one network to the other.

Hi everyone,

In this post I am sharing a PowerCLI script that I made in order to easily migrate VMs from one network to the other.

There are two versions of this script, one version has a hard-coded ESXi/vCenter instance, and the other one asks for the vCenter on each run.

Version 1 (hard coded ESXi/vCenter):

Do {
$vspherehost = "ADD-ME"
$src = Read-Host -Prompt "Input source network"
$dst = Read-Host -Prompt "Input destination network"

Connect-VIServer $vspherehost
Get-VM | Get-NetworkAdapter | where{$_.NetworkName -like $src} | Set-NetworkAdapter -NetworkName $dst
} while($true)

Version 2 (Asks for ESXi/vCenter on reach run):

Do {
$vspherehost = Read-Host -Prompt "Input vCenter appliances or ESXi host"
$src = Read-Host -Prompt "Input source network"
$dst = Read-Host -Prompt "Input destination network"

Connect-VIServer $vspherehost
Get-VM | Get-NetworkAdapter | where{$_.NetworkName -like $src} | Set-NetworkAdapter -NetworkName $dst
} while($true)

As you can see, I opted for a while loop so I can change multiple networks at once, because it simply runs the script again for me instead of me having to manually run it again.

The script is not optimized as you can see, for any improvements or ideas, please email me using my contact page.

Thank you for reading and I hope to see you soon in a new post which will be about the vExpert program.

About Author