PowerCLI command to list all VMs in a cluster

A quick PowerCLI command to list all VMs in a cluster:

Get-Cluster "Cluster Name" | Get-VM

To sort by name run:

Get-Cluster "Cluster Name" | Get-VM | Sort Name

To export to a csv file run:

Get-Cluster "Cluster Name" | Get-VM | Export-CSV C:\ListVMsInCluster.csv

The commands above are very simple and because I am so forgetful I need them somewhere I can quickly find them.

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.