I had worked on an automation project to ease up virtual machine provisioning – for that it was necessary to create a script that would list out all the networks that is visible to an ESXi host (so that the correct one could be added to the new virtual machine). Turns this was quite easy with the Get-VirtualPortGroup cmdlet – here’s how to do it (its so simple that its going to be a smaller than normal post)
$host_info = Get-VMHost -VM $vmname
Get-VirtualPortGroup -VMHost $host_info.name
And that’s it! no magic to it at all!