A lot of people see potential with PowerCLI, but struggle to get started
because PowerShell is a fairly new technology and not too many people know it
yet. If you’re getting started, here are my recommendations.
If you’re getting your hands on PowerShell for the very first time, I’d
recommend working through the first 3 or 4 sections of the PowerShell
owner’s manual, this will get you to the point where you can launch
PowerShell and are ready to run some commands.
After that, to master PowerShell you’ll have to master these two separate
areas:
- The PowerShell language.
- PowerShell cmdlets.
Know how to get help.
First, if you’re an absolute beginner, start by mastering PowerShell help.
Spending a few hours here will save you a lot of time later on. PowerShell
cmdlets are self-documenting, so the online help is accurate and definitive. It
also shows you critical facts about cmdlets, such as what arguments they accept,
what input types are needed, and what can be piped in to the cmdlet. There’s a
great intro on using help over at concentratedtech, and
you should also check out MSDN.
Use these guides, play around with the cmdlets, then try these same
principles with a few cmdlets of your own, you won’t regret it.
The key things to remember are “help <cmdlet name> –full” to get all
information about a particular cmdlet (example “help get-vm”) or “help
<cmdlet name> –examples” to get just the examples. One of the great things
about PowerShell is that help cmdlet –examples almost always yields a useful
example or two.
This works great for cmdlets, but unfortunately the situation is not so
simple for things that are part of the PowerShell language itself, like the if
statement or comparison operators. We’ll talk about how to get a handle on those
later.
The top 25 cmdlets worth knowing.
Anyway there are a lot of cmdlets and only so much time in the day. You may
be wondering where to focus your attention. There’s a great online of repository
of PowerShell code over at poshcode.org.
Recently I analyzed all the code on poshcode to count the frequencies of various
cmdlets and operators.
So here are the top 25 cmdlets used by code over at This sample is drawn from a lot of different authors all trying to accomplish different goals, so it's a good cross section of the most useful cmdlets. Note however that I would recommend that beginners not learn add-member until later, even though it is obviously a very useful cmdlet for experts.
Poshcode.org most popular cmdlets.
Rank |
Cmdlet |
Cmdlet |
1 |
add-member(*) |
4862 |
2 |
foreach-object |
4039 |
3 |
write-host |
3696 |
4 |
write-output |
2494 |
5 |
new-object |
2139 |
6 |
out-file |
1669 |
7 |
where-object |
1550 |
8 |
sort-object |
1473 |
9 |
get-content |
1399 |
10 |
get-childitem |
1187 |
11 |
get-command |
805 |
12 |
select-object |
754 |
13 |
read-host |
693 |
14 |
get-history |
693 |
15 |
set-variable |
682 |
16 |
write-debug |
657 |
17 |
invoke-expression |
576 |
18 |
test-path |
501 |
19 |
split-path |
471 |
20 |
get-process |
436 |
21 |
add-content |
408 |
22 |
get-wmiobject |
383 |
23 |
get-member |
379 |
24 |
start-process |
370 |
25 |
write-verbose |
354 |
(*) This cmdlet ranks high due to the nature of the code on
poshcode.org, it’s a useful cmdlet to know but you should wait until you’re an
expert to really dig in to it.
If you’re starting out, you should begin by mastering most of these cmdlets
(you should skip add-member until you’ve become a master in your own right).
Remember, each of these has samples within its help that will get you on your
way. There’s one more cmdlet that VMware admins in particular get a lot of use
from, namely Measure-Object. If you’re mainly going to use PowerShell to manage
VMware, that one should be in your top 10 because our users use this cmdlet a
lot.
Operators and other language features.
As I mentioned, you’ll also need to master the PowerShell language. This
includes learning how to branch using if/then/else, how to build loops with
while and foreach, etc. For example, consider this code:
1 |
<span style="color: #0000ff;">if</span> ($x –eq 1) { |
1 |
write-host <span style="color: #006080;">"x is 1"</span> |
1 |
} <span style="color: #0000ff;">else</span> { |
1 |
write-host <span style="color: #006080;">"no it isn't"</span> |
1 |
} |
So how, exactly, do if and else work? Help is available for all these things,
but in some cases it’s hard to track down. PowerShell’s help contains a lot of
topics in the form of about_something. For example, there is an
about_if, which talks about both the if and else statements. You can see all of
them by typing “help *about*”. The other thing to note in the statement above is
the use of “-eq”. This is a so-called comparison operator, and mastering these
is very important. Type “help about_comparison_operators” to learn all about
these. Also, here are the top comparison operators as found on poshcode.org:
Poshcode.org most popular operators.
Rank |
Operator |
Frequency |
1 |
-eq |
4480 |
2 |
-gt |
1329 |
3 |
-and |
1320 |
4 |
-match |
924 |
5 |
-ne |
775 |
6 |
-contains |
582 |
7 |
-not |
514 |
8 |
-lt |
506 |
9 |
-or |
480 |
Lastly but not leastly, you’ll need to master the pipe. This tends to be
pretty easy because once you start piping it will seem very natural to you. Just
for reference, our trusty vertical friend appears 15491 times.
Getting really serious.
Another great little tutorial is the Top 10 Tips for Using Windows PowerShell. But if you’re really serious about upgrading your PowerShell skills,
you should consider getting a hold of either Hal Rotterberg’s Managing VMware Infrastructure
with Windows PowerShell TFM and Bruce Payette’s Windows
PowerShell in Action. Bruce’s book is the best from a theory perspective,
while Hal’s book lets you learn while you improve the way you manage VMware.
If you’re having trouble and need more help, don’t forget that help is
available in the PowerCLI
community.