Let’s take a look at what are basic VMware PowerCLI commands including connecting, PowerCLI commandlets, and looping. Previously, we have looked at VMware PowerCLI and how to download and install PowerCLI and integrate it into Windows Powershell ISE.
Basic VMware PowerCLI Commands
Let’s have a look at some basic PowerCLI commands to see how easy it is to initiate traction information from the vSphere environment with PowerCLI. Please bear in mind, the below commands in no way meant to be an all-embracing guide, but rather an introduction to VMware PowerCLI and how to get started running commands and learning to automate your environment.
The basic commands we need to run is the one to actually connect to the vSphere environment. You can either connect directly to vCenter Server or to an ESXi Host.
To connect, run the command below in a PowerCLI-enabled PowerShell session.
To Connect ESXi with PowerCLI
Connect-VIServer –Server [Name or FQDN]
When you run the command, you will be received your login credentials.

Enter the username and password for either your vCenter Server or ESXi Host.

ESXi host successfully connected.

We use the get-vm command to get information about the guest VMs.
Get-VM

Basic VMware PowerCLI Commands
To receive more information from the get-vm commandlet, we can show the complete information with the command.
Get-VM | fl
Displays more information such as the Operating System, VM Hardware version level, Resource pools, the folder it is located in, and many more.

Basic PowerCLI Scripting
Let’s find the virtual machines that are poweredoff. PowerShell commandlet.
Get-VM | where-object {$_.PowerState –eq “PoweredOff”}

How to Start VM using PowerCLI Command
As we can see, this displays only the virtual machines that are “PoweredOff”. This can be extremely helpful. Now follow the below commandlet to power on VMs.
Get-VM | where-object {$_.PowerState –eq “PoweredOff”} | Start-VM

Stop Guest VMs
How about shutting down VMs? We can do that by following the below command.
Get-VM 〈yourvm〉 | Stop-VMguest

If you don’t need to get the confirmation of the action, you can add the –confirm:false parameter

Let’s see how many VMs are running.
Get-VM | where-object {$_.NumCpu –gt 1}

Related: How to Backup ESXi Configuration


