Get a list of VMs on a vSphere host using PowerCLI
PowerCLI is a PowerShell snap-in that allows you to perform advanced scripting against either vCenter or individual vSphere hosts. Let’s assume that you have Microsoft PowerShell installed on a Windows system and you have downloaded and installed the latest version of VMware’s PowerCLI snap-in for PowerShell. That will be our starting point.
If you haven’t already, execute the command: set-executionpolicy remotesigned
This command configures PowerShell to run local scripts without hassling you but requires that scripts downloaded from the Internet be signed by a trusted publisher. If you don’t execute this command, you will get errors in PowerCLI that can affect script execution.
As I mentioned, vCenter isn’t required to run PowerCLI. You can connect directly to a vSphere host against which you can run a wide variety of cmdlets (the PowerShell term for commands). The cmdlet that initiates a connection to your vSphere host is Connect-VIServer.
When using this Connect-VIServer cmdlet, you need to specify the name or IP address of your vSphere server, the connections protocol – HTTP or HTTPS – you wish to use and the username and password for a user with enough rights to poll the server for statistics. For my lab server, I use the following command:
Connect-VIServer -Server 192.168.200.1 -Protocol https -User root -Password passwordgoeshere
If the connection is successful, you will be presented with information about the connection.
You need to connect to a server before you can perform operations against it.
Now, execute the command “get-vm | fl” and press enter. The get-vm command will retrieve a list of running virtual machines on the host. The “fl” portion of the command stands for format-list and shows you the output in list form as shown below. Note that you can see a lot of details about each virtual machine, including memory, CPU count and power state.
There you are! A list of virtual machines on a vSphere host.