I made a simple script for 1 job : changing the portgroup for multiple vm’s in the same portgroup.
The script will ask which portgroup you want to move vm’s from using a selection after that using the same method it will ask you for the portgroup to move to. Then it will change all the vm’s using that portgroup to the new one. Pretty simple but if you are a little bit smart about it you can change this to do even more and maybe make it a bit prettier because now you need to put in the vcenter name manually in the script.

I made 2 versions of this, one for standard switch and one for Distributed switch which i will both add below.

Standard Switch Script

Connect-VIServer VCENTERNAME

$pg = Get-VirtualPortGroup | Out-GridView -OutputMode Single -Title "Select Current Portgroup"
$pgName = $pg.Name

$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name=$pgName}

$vmlist = Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name


####################################

$newpg = Get-VirtualPortGroup | Out-GridView -OutputMode Single -Title "Select New Portgroup"
$newpgname = $newpg.Name

foreach ($vm in $vmlist){
    Get-VM -Name $vm |  Get-NetworkAdapter | where{$_.NetworkName -eq $pg.Name} |  Set-NetworkAdapter -NetworkName $newpg.Name -Confirm:$false
}

Distributed Switch Script

Connect-VIServer VCENTERNAME

$pg = Get-VDPortGroup | Out-GridView -OutputMode Single -Title "Select Current Portgroup"
$pgName = $pg.Name

$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name=$pgName}

$vmlist = Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name


####################################

$newpg = Get-VDPortGroup | Out-GridView -OutputMode Single -Title "Select New Portgroup"
$newpgname = $newpg.Name

foreach ($vm in $vmlist){
    Get-VM -Name $vm |  Get-NetworkAdapter | where{$_.NetworkName -eq $pg.Name} |  Set-NetworkAdapter -NetworkName $newpg.Name -Confirm:$false
}

The original article was posted on: www.hollebollevsan.nl

Related articles

  • Cloud Native
  • Application Navigator
  • Kubernetes Platform
  • Digital Workspace
  • Cloud Infrastructure
  • ITTS (IT Transformation Services)
  • Managed Security Operations
  • Multi-Cloud Platform
  • Backup & Disaster Recovery
Visit our knowledge hub
Visit our knowledge hub
Paul van Dieën Virtualization Consultant

Let's talk!

Knowledge is key for our existence. This knowledge we use for disruptive innovation and changing organizations. Are you ready for change?

"*" indicates required fields

First name*
Last name*
Hidden