VMware’s PowerCLI can come in quite handy in a vSphere environment, especially when you are managing a large number of hosts. Sure, you can find sample scripts on the Internet to run, but it is also a good idea to at least get a fundamental understanding of how PowerCLI works. After all, you would not want to let a script loose in your environment if you did not have any idea what it did.
The good thing about PowerCLI is there are a number of features built right in to help you get going. I like to learn by doing, so here are some ideas on how to fumble through PowerCLI to get started. This is your self learning guide to VMware PowerCLI, from finding your PowerCLI download to connecting to vCenter with PowerCLI and beyond!
First things first…
How to Download VMware PowerCLI
First things first, you will need to download VMware PowerCLI and install PowerCLI to start using it. PowerCLI requires PowerShell to already be installed one the system you plan on using. The best place to download VMware PowerCLI from is the VMware Code site. You can easily download the latest versions, as well as previous versions of VMware PowerCLI.
As you can see, VMware PowerCLI made a huge jump in versions from VMware PowerCLI 6.5.4 to VMware PowerCLI 10.0.0. While I do not work for VMware, so I do not know their logic for this jump in versioning, my best guess is because the features and functionality of PowerCLI 10.0.0 have increased so dramatically.
VMware PowerCLI 10 now supports Mac OS X and Linux operating systems. Wow! With a couple prerequisites, you can be administering your VMware vSphere environment from any operating system you would like!
You must first install PowerShell Core on your system, and then you will be able to install VMware PowerCLI on Mac or install VMware PowerCLI on Linux. For instructions on how to do this, be sure to take a look at the VMware PowerCLI Blog.
Getting Started With VMware PowerCLI Basics
Now that you have installed VMware PowerCLI on your system of choice, you are ready to get started!
Use PowerCLI to Connect to vCenter
If we’re talking about basic PowerCLI, then using PowerCLI to connect to vCenter is as basic as you can get.
I am going to connect to my vCenter server by using the following command:
Connect-VIServer is a command you are going to use over and over, so make sure to commit it to memory! After we have connected to vCenter with PowerCLI we will start with the basics by listing our hosts and virtual machines.
Something to remember is that you do not connect to your host, you connect to your virtual machine host. Otherwise, simply typing
Get-Host won’t get you anywhere, because it is just going to tell you about the system you are running PowerCLI on.
So what else can we do? Type
Get-VICommand to see the commands you can choose from. You are going to end up with a very long list of things you can do with PowerCLI.
Let’s take a closer look at
Get-VM in PowerCLI.
Get-VM command to get started.
Here is the very basic information about all of the VMs in the vCenter you are connected to.
You can get quite a bit more with the PowerCLI
Get-VM | Get-Member to see all of the properties and methods that can be returned by the Get-VM command.
Theres’s so many options here, that I won’t list them. Try it for yourself to see what you can find!
Get-VM in PowerCLI is one of the most useful commands at your fingertips.
Here’s another really useful PowerCLI snippet.
Run the command
Get-VM | Sort PowerState to sort your VMs by which ones are powered off and powered on.
Use PowerCLI to Shutdown VM
Now that you see which VMs are on and off, what if you want to use PowerCLI to shutdown a VM?
To shutdown a VM with PowerCLI we are doing an operation on the VMGuest. To see what commands are available for a VMGuest, type Get-VICommand *vmguest* as shown below.
I’m pointing this one out because PowerCLI can be a little weird sometimes. I know my first instinct was to jump to the Set-VM command, since I could get the PowerState from Get-VM.
As it turns out, that is not the case.
To shutdown a VM with PowerCLI type:
Shutdown-VMGuest -VM yourvmname
Guss what? Shutdown-VM guest is something called an alias in PowerCLI. You could use the
Stop-VMGuest command and achieve the exact same results.
Now, let’s look at another way to learn more about PowerCLI!
Learning PowerCLI Basics with vSphere Networking
One of the most common tasks a vSphere administrator will face is to configure networking on an ESXi host. PowerCLI can make this task quite a bit easier, so we are going to focus on vSphere Networking PowerCLI commands to teach ourselves, since there are so many different things you can do with powerCLI.
To try to narrow things down, let’s look for some commands we can run on virtual switches by typing
How do we find out more about our switches? That depends on the type. You will see
VDSwitch for Distributed vSwitches, and
VirtualSwitch for Standard vSwitches. Finding out more about our Standard vSwitches is easy. We can use
Get-VirtualSwitch to find out what switches we have in our environment.
Notice we also get information on Distributed vSwitches, but PowerCLI is nice enough to warn us that information may not be around forever and we should use
PowerCLI is not case sensitive, as illustrated above, so you can type your commands in lower case, upper case, or any combination you feel like. PowerCLI also features tab complete, once you’ve started typing a command. Hit tab to cycle through commands with the prefix you’ve typed until you find the one you’d like.
We can be a lot more specific than just listing the switches in our environment. To find out how to use the
Get-VirtualSwitch command, type
You will be able to find out what the command does, as well as the syntax. PowerCLI will also tell you how to get more information on the command, such as examples and the full details by using
Get-Help Get-VirtualSwitch -examples and Get-Help
Just running Get-VirtualSwitch gives us some information, but not many details. To get more details about our switches, type
Get-VirtualSwitch | Get-Member
Get-VirtualSwtich | Format-Table
Now we have some information on our switches formatted in an easy to read table. We’ve got duplicates of vSwitch0 and vSwitch1, so it is hard to say where those switches reside. Let’s take a look at our output from
Get-VirtualSwitch | Get-Member, and add some attributes to our table.
Now we can see which host each vSwitch1 and vSwitch0 are on. Since the other switches are distributed switches, they don’t have a host listed in the table.
So you’ve been playing around with PowerCLI and getting a feel for things. What comes next? Getting started with PowerCLI is always easiest when you think in the context of daily administrative tasks. Besides giving you some common use cases, you’ll learn new ways to take a look around your vSphere environment with PowerCLI and be able to relate it to what you are used to doing within the vSphere client.
After getting used to finding information, you’ll find yourself beginning to write scripts and one-liners to be able to get and set specific information. This can be the fist step in saving time and effort in big ways with scripting and automation on PowerCLI.
This article is part of the Fumbling Through PowerCLI Series.
Part 1 – A Guide to Fumbling Through PowerCLI
Part 2 – Continuing to Get Set With PowerCLI and Standard vSwitches
Part 3 – Get Set With PowerCLI and Distributed Virtual Switches, Part 1
Stay tuned for more fumbling through PowerCLI, focused on configuring vSphere networking components. Remember, when in doubt, you can always
Melissa is an Independent Technology Analyst & Content Creator, focused on IT infrastructure and information security. She is a VMware Certified Design Expert (VCDX-236) and has spent her career focused on the full IT infrastructure stack.