Change the Network Switch Attached to Multiple VMs with PowerShell: Connect-vmnetworkadapter

When I import many VMs I often need to change the name of the network switch (virtual switch) they’re attached to as I host multiple copies of the same VMs for labs. Here’s a snippit I use to make that happen

$VMNamePatterns="ProjectName”
$OldNetworkName = "Contoso Network"
$NewNetName = "Test Contoso Network"

Get-VM | where vmname -like $VMNamePatterns* | Get-VMNetworkAdapter | where switchname -EQ $OldNetworkName  | Connect-VMNetworkAdapter -SwitchName $NewNetName

Again this is quite simple, I get all my VMs filter by the project name, then get their attached adapters and filter by the switches they’re attached to and update the connection. Simple but it saves me about 30 minutes every time I use it.

What do you think?