PowerCLI command to list portgroups of VMs in datacenter

Prior to performing network segment changes of multiple VMs for a specific customer I wanted to print out a list of all the VMs and their associated network/portgroups. To get this info I ran the following command:

Get-DataCenter DataCenterName | Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}},@{N="NIC";E={$_.Name}},@{N="Network";E={$_.NetworkName}}

You can then perform the relevant network changes and run the command above again to compare pre and post results. To export the results to a CSV file run the following:

Get-DataCenter DataCenterName | Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}},@{N="NIC";E={$_.Name}},@{N="Network";E={$_.NetworkName}} | Export-csv C:\VMPortGroups.csv

You can change the object being queried by replacing Get-DataCenter DataCenterName with another object such as a cluster with Get-Cluster ClusterName.