A PowerCLI Primer – Part 1

Table of contents





For those of you following this blog, you probably may have come across some of the PowerCLI-centric posts I wrote so far. These generally revolve around a script I cooked up to automate some task just to avoid going down the manual route which frequently leads not only to human error but to brain numbness induced out of sheer boredom. Two such examples are the ones titled How to find orphaned vSphere vms using PowerCLI and Fixing VM folder naming discrepancies.

I am no PowerCLI guru myself but come to think of it, I’ve always assumed a certain degree of familiarity and knowledge on the reader’s part when writing about PowerCLI. This made me realize that I just might have overlooked those of you who have never heard of PowerCLI or have just begun using it. This two part post will hopefully make amends by laying out the basics in a Q&A format giving examples along the way. So, without further ado, let’s begin.

If you already know the basics of PowerCLI, why not give our PowerCLI Hacks a try? 25 Scripts directly to your inbox, check them out here!


What is PowerCLI?

It all starts with Microsoft’s PowerShell this being a scripting language shipped with practically all recent flavors of the Windows operating system. PowerShell relies on the .NET framework and comprises a series of so called cmdlets and functions. Cmdlets are small programs written in compiled .NET. Functions, on the other hand, are written in the PowerShell language but are compiled only when required.

At this point all you need to know is that VMware released PowerCLI as an extension to PowerShell. In doing so, VMware provides its own set of commands specifically written to target VMware products such as ESXi and vCenter Server. In the rest of the post please assume that the word command includes both cmdlets and functions.


How do I install PowerCLI?

You can download the PowerCLI installer from the my.vmware.com website. You must register for a user account if you do not have one. Installing it is a matter of running the installer by double clicking on it.


What is the latest version of PowerCLI?

Following the recent release of vSphere 6.5, the latest version of PowerCLI at the time of writing is 6.5 Release 1.


What are the prerequisites?

If you’re planning on installing, say, the latest release of PowerCLI, you’ll be needing at least PowerShell 3.0 and .NET Framework 4.5 or later. You must also be running a 64-bit Windows flavor from the following list;

  • Windows Server 2012 R2
  • Windows Server 2008 R2 Service Pack 1
  • Windows 10
  • Windows 8.1
  • Windows 7 Service Pack 1


How do I check if PowerCLI is already installed?

The easiest way to check if PowerCLI is present on your computer is by looking at the list of installed software under Programs and Features in Control Panel.

Figure 1 - Verifying the currently installed version of PowerCLI

Figure 1 – Verifying the currently installed version of PowerCLI


How can I check which version of PowerCLI I’m running?

You can check the version by looking at the Version column as per Fig. 1 above. In this case, I have version 6.3 installed. Alternatively, fire up a PowerCLI console by clicking on the PowerCLI taskbar icon as shown in Figure 2. Depending on the version of Windows, you should also find a shortcut to PowerCLI under the VMware group on the start or apps menu.

Figure 2 - PowerCLI icon on the Windows taskbar

Figure 2 – PowerCLI icon on the Windows taskbar

Once you do this, run Get-PowerCLIVersion as shown in Figure 3. You will find the complete version details under the PowerCLI Version header.

Figure 3 - Retrieving version details on the currently installed PowerCLI build

Figure 3 – Retrieving version details on the currently installed PowerCLI build


How do I list all the available VMware commands?

You can run the PowerShell Get-Command from a PowerCLI session. Just remember that PowerCLI is running atop PowerShell, meaning that PowerShell syntax and commands will work just as good.

Example 1 – List all VMware related commands.

get-command | where {$_.Source -like 'VMware*'}

Example 2 – List all VMware cmdlets that contain the text ‘Datastore’. Note that you can add where {$_.Source -like ‘VMware*’} to filter out non-VMware related cmdlets.

get-command -Type cmdlet -name '*datastore*'


How can I learn more about a specific PowerCLI command?

Use the Get-Help PowerShell cmdlet to display usage and syntax details as well as the description for the particular command.

Example 1 – Display the help page for the cmdlet Connect-VIServer.

Get-Help Connect-VIServer

Example 2 – Display the online help page for the cmdlet Disconnect-VIServer.

Get-Help Connect-VIServer -Online

Example 3 – List usage examples for a specific command.

Get-Help Get-VM -Examples


How do I connect to an ESXi host or vCenter Server?

To carry out anything useful when using PowerCLI, you first need to establish a connection to either an ESXi host or a vCenter Server.

The Connect-VIServer cmdlet is used to this effect. Note that you can have a number of concurrent connections if the command is repeatedly used to connect to different targets.

Example 1 – Connect to an ESXi host using integrated authentication (currently logged on user). A credentials box is displayed whenever command line credentials are omitted


Example 2 – Connect to an ESXi host using credentials passed on from the command line.

Connect-VIServer -User root -Password Notapassword

Example 3 – Connect to a host or vCenter Server using the -menu parameter to select an entry from a history list (a list of previously established connections).

Connect-VIServer -menu
Figure 4 - Connecting to a previously connected to ESXi host or vCenter Server

Figure 4 – Connecting to a previously connected to ESXi host or vCenter Server


How do I retrieve a list of connections currently in use?

There’s a variable called DefaultVIServers which keeps a list of connections established during a current session. We can display the contents by simply calling the variable as follows;

Figure 5 - Displaying currently established ESXi or vCenter Server connections

Figure 5 – Displaying the list of currently established ESXi or vCenter Server connections


How do I disconnect from an ESXi host or vCenter Server?

Use the Disconnect-VIServer cmdlet to drop a connection to ESXi or vCenter Server.

Example 1 – Drop all established connections.

Disconnect-VIServer -Server * -Force

Example 2 – Drop a specific connection using the $DefaultVIServers variable. The -Confirm:$false parameter is used to suppress confirmation. Note that the variable in reality is an array, with [0] represtnign the first entry in the list, [1] the second and so on and so forth.

Disconnect-VIServer -Server $DefaultVIServer[0] -Confirm:$false

Example 3 – Drop a specific connection by specifying the IP address or hostname of an ESXi host or vCenter Server.

Disconnect-VIServer -Server vcsa-2.vsphere.local -Confirm:$false


How do I retrieve a list of virtual machines hosted on an ESXi host?

The get-vm cmdlet is used to list vms hosted on an ESXi host.

Example 1 – List all the vms on a specific ESXi host with IP address

Get-VM -server

Example 2 – Generate a list of vms displaying only the VM name and running state. If you have connected to multiple hosts, the combined list of hosted vms will be retrieved.

Get-VM | ft -Property Name,PowerState

Example 3 – List only those vms that are currently powered on.

Get-VM | where {$_.PowerState -eq 'PoweredOn'}

Example 4 – List only those vms that are running a Windows guest OS.

get-vm | where {$_.Guest.OSFullName -like '*Windows*'}



If you are just starting out with PowerCLI, this post should have given you the basics to set you off on the right track. In part 2, I’ll dig a little bit deeper though I promise to keep it as simple and easy to follow as possible.

In the meantime, I suggest visiting a couple of PowerShell related sites just so you get acquainted with the syntax and everyday use. Personally, I find the Windows PowerShell Scripting site to be a very good resource.

Make sure to watch this space for part 2. If you’re eager to learn more you can re-visit the PowerCLI starter series by Luke, or subscribe to 25 PowerCLI hacks prepared by Adam Bertram!


[the_ad id=”4738″][the_ad id=”4796″]

Altaro VM Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

Leave a comment

Your email address will not be published.

Microsoft 365 Security checklist - free eBook