Use PowerCLI to get VMs and the portgroups they are in using PowerShell and vCenter

Problem:

How do I see which port groups my VMs are in?

Solution:

Run the below PowerShell code:

Connect-VIServer servername -User -Password 

Get-VM |

Select Name,PowerState,Version,NumCPU,MemoryGB,ProvisionedSpaceGB,UsedSpaceGB,

    @{N='GuestOS';E={$_.Guest.OSFUllName}},

    @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

    @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},

    @{N='Cluster';E={(Get-Cluster -VM $_).Name}},

    @{N='HostName';E={$_.VMHost.Name}} |

    Export-Csv C:\temp\VMPortGroup.csv 

Leave a Reply

Your email address will not be published. Required fields are marked *