Let’s take a look at the 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 that the below commands are in no way meant to be an all-encompassing guide but rather an introduction to VMware PowerCLI and how to get started running commands and learning to automate your environment.
The basic command we need to run is the one to actually connect to the vSphere environment. You can either connect directly to the 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 receive your login credentials.
Enter the username and password for either your vCenter server or ESXi host.
The 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
It 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 powered off. 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 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