VMware vSphere PowerCLI extends PowerShell with cmdlets specifically for managing VMware servers. PowerCLI includes more cmdlets than PowerShell itself ships with, giving administrators broad access to VMware’s internals. The downside of this wealth of tools is that it’s easy to get lost inside PowerCLI if you don’t start with specific goals in mind.
In this article, I walk through an entire PowerCLI working session that includes connecting to a server, performing some basic monitoring and management tasks, and disconnecting. The steps for using PowerCLI are always basically the same, so my example not only provides you with a framework for understanding PowerCLI but also demonstrates essential PowerCLI features. I also direct you to some of the most useful resources for discovering how to use PowerCLI for more specific tasks.
Prerequisites
To manage a VMware environment with PowerShell, your environment must meet three requirements. First, you need to have a VMware server environment available. VMware PowerCLI works with VMware ESX or ESXi 3.0 and later, as well as any VMware Server, VMware vCenter Server, or VMware vSphere product, version 2 or later.
Second, you need a workstation with PowerShell 2.0 installed; this is where you’ll install PowerCLI. As of press time, the current version of VMware vSphere PowerCLI is 4.1.1. To get the installation package, go to the VMware vSphere PowerCLI homepage. Alternatively, you can go to the VMware Download Center and search for PowerCLI. If you haven’t already registered with VMware, you’ll need to do so—the download is free. Please be aware that there’s a similarly named but distinct product: vSphere CLI. Although vSphere CLI also allows command-line VMware management, vSphere CLI is a traditional console application, not the PowerShell toolkit. Make sure you download PowerCLI.
The third requirement is that you need access to the VMware server from the workstation you’ll use for management. This might seem self-evident, but I mention it separately because there’s minimal work for remote access to a VMware server. If you have access to the VMware server’s network via VPN or similar technology, there’s nothing specific you need to configure for access; you simply need to connect to the server’s network. If you don’t have VPN access but can configure port forwarding on the router for the VMware server’s network, log on to the router and forward a port on the public side of the router to the VMware server’s management port (443 by default). You can then use the public IP address or DNS name of the network and the public port number to connect to the server.
Loading PowerCLI
In general, you don’t need to worry about the details of loading PowerCLI. The VMware PowerCLI installer provides you with a startup shortcut on the Start menu under VMware, VMware vSphere PowerCLI. When you start PowerShell from that shortcut, PowerShell automatically runs a configuration script that loads the PowerCLI snap-in (among other things). If you always use the VMware PowerCLI shortcut to start a PowerCLI session, you can skip to the next section.
If not using the shortcut, you can load the PowerCLI snap-in directly at a PowerShell prompt; you just need to know its name (i.e., VMware.VimAutomation.Core). Use this name with PowerShell’s Add-PSSnapin cmdlet, like this:
Add-PSSnapin VMware.VimAutomation.Core
PowerShell will automatically find the snap-in and load it for you. This works from a script as well. If you try to use this command on a machine without PowerCLI installed, you’ll get an error message similar to this: “The Windows PowerShell snap-in ‘VMware.Vim.Automation.Core’ is not installed on this machine.” (As an aside for scripters, after running this command you can see if it succeeded by checking the value of the $? variable; it will be false if the snap-in didn’t load.)
If you’re loading the snap-in yourself instead of using the PowerCLI shortcut, I suggest that you at least glance at the configuration script. This script, Initialize-PowerCLIEnvironment.ps1, is in PowerCLI’s installation folder. Beyond loading the snap-in and customizing the PowerShell interface, the configuration script also defines several PowerCLI-specific aliases and functions.
Connecting to a VMware Server
After you start the Power CLI session, use the Connect-VIServer cmdlet to connect to the VMware server you’re managing. This step is typically very simple; you just use the Connect-VIServer cmdlet with the server name or address:
Connect-VIServer -Server 192.168.1.21
PowerShell will then prompt you for credentials to let you connect to the VMware server using a standard PowerShell credentials dialog such as the one that Figure 1 shows.

Figure 1: Entering PowerShell credentials
There’s one situation in which this approach might not work. Suppose you’re tunneling through the Internet to the remote VMware server. The best standard solution to this problem isn’t to do something with PowerCLI, but to set up a secure VPN to the remote site. If that’s not an option, however, you can handle it with some router reconfiguration and the Connect-VIServer cmdlet.