Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Read
through these pages to get familiar with Windows PowerShell, and soon
you¶ll be driving around like a pro. c
×
A couple years ago one of the Scripting Children got his driver¶s permit; that
same day, the Scripting Dad took his son out for his first driving lesson. As
you might expect, the Scripting Son was pretty excited and couldn¶t wait to
get to the parking lot where the lesson would take place. After parking the
car, the Scripting Dad and the Scripting Son changed places. The Scripting
Son started the car, dutifully checked his mirrors, released the parking
brake, and shifted the car into Drive. And then, for a moment or two, he
paused before finally stepping on the gas and driving a car for the first time.
Why the pause? That¶s easy: it¶s one thing to want to drive a car, but it¶s a
whole µnother thing to actually m a car. You can¶t blame the Scripting
Son for hesitating a bit; after all, for years he¶d been riding a bike, and he
was able to get pretty much any place he needed to ge t to. Why change
now? Why discard a tried-and-true mode of transportation for something
new, something that he might not like, something that might be too hard?
Admittedly, cars have some advantages over bikes. But at what cost?
The same thing is true when it comes to new technologies designed for
system administration. By now most of you have heard of Windows
PowerShell, Microsoft¶s new command console and scripting language;
many of you have even downloaded and installed PowerShell. (If you
haven¶t, see our ë
page for more
information.) Like the Scripting Son, however, you might have gotten that
far and then paused: after all, it¶s one thing to want a new command
console and scripting language, but it¶s a whole µnother thing to actually
start p that command console and scripting language. You¶ve been
using the î I, Cmd.exe, and VBScript for years, and you¶ve pretty much
been able to carry out any system administration task you needed to carry
out. Why change now? Why discard a tried -and-true mode of system
management for something new, something that you might not like,
something that might be too hard?
Those are all good questio ns and all valid concerns. And, to be honest, we
can¶t really address them all in this introductory article. However,
we say this: just because you get a driver¶s license that doesn¶t mean
that you can never ride a bike again. Transportation is not a ³zero-sum´
proposition: having a driver¶s license doesn¶t mean you can¶t also ride a
bike, a bus, or a motorcycle. (Or even - gasp! - .) Would you rather
ride a bike than drive a car? That¶s fine; ride a bike when you want to ride a
bike, and drive a car when you want to drive a car. If it turns out that
you¶re happier riding a bike, well, good for you: ride your bike, and drive a
car only when you really need to.
You should think of Windows PowerShell in the same light. If you learn
Windows PowerShell, does that mean you have to stop using the î I and
you have to throw out all your VBScript scripts? Of course not. Many people
believe that Windows PowerShell is a replacement for VBScript and other
management technologies. That¶s not really true; if anyth ing, PowerShell is
a
to VBScript and other management technologies. Over time,
some people will decide, ³Wow, I really like PowerShell; I think I¶m going to
use PowerShell for everything.´ That¶s fine; have fun and let the Scripting
îuys know how we can help. Other people are going to think, ³I don¶t
know. I like some of the things PowerShell has to offer, but I¶ve got a pretty
big investment in VBScript scripts. I¶d rather stick with VBScript.´ Well,
guess what? That¶s also fine. As the saying goes, if it ain¶t broke, don¶t fix it.
If VBScript works for you then keep using VBScript.
So then why even bother with Windows PowerShell? There are several
reasons. For one, it¶s like having a driver¶s license: you might never need it,
but it¶s good to have, just in case. It¶s true that, at the moment, PowerShell
might not seem like that big of a deal: for the average system administrator
pretty much everything you can do with Windows PowerShell you can
already do with VBScript. On the other hand, however,
there exceptions: if you want to automate the management of Microsoft
Exchange Server 2007 you¶ll need to use PowerShell. As time marches on
these ³exceptions´ are likely to become the rule: PowerShell will continue to
be enhanced and improved upon (v ersion 2.0 is in development right now)
while VBScript won¶t. Admittedly, you might not need PowerShell right now;
however that could change in the very near future. So why not go ahead
and learn it now, when you can take your time and learn at your pace?
ü . For better or worse, this particular article can only provide a somewhat-cursory introduct
drill down deeper and deeper into PowerShell and what you can do with it; if you can¶t wait till th
îuys¶ five
.
Top of page
When you install Windows PowerShell the setup program should also install
a Start Menu shortcut for you; in Windows XP and Windows Server 2003
that shortcut can be found by clicking
, pointing to
,
pointing to
, and then clicking
. What if you mm get a Start Menu shortcut? Well, PowerShell
installs, by default, in m \System32\WindowsPowerShell\v1.0 (e.g.,
C:\Windows\System32\WindowsPowerShell\v1.0). Open that folder, double -
click
and have at it.
When you m start Windows PowerShell you¶ll see something similar to this:
c
If you¶re thinking, ³Hmmm, that looks a lot like a good old MS -DOS
command window,´ well, there¶s a reason for that: PowerShell actually
leverages the good old MS-DOS command shell. And there¶s at least one
advantage to that. Because you¶ve probably used the old MS -DOS command
shell a million times you already have a pretty good idea of how to use
PowerShell: you just type in commands and press ENTER. For example , do
you need to know your IP address? Then type and press ENTER:c
c
c
c
c c
c
ccccccccc cccccccccccccc
c
c c c
cccccccc
c!c"cccc#$$ c
cccccccc c
ccccccccccccc%&'%()%'*%+ c
cccccccc#
c
ccccccccccccc',,',,',,* c
cccccccc! c- .ccccccccccc%&'%() %'*%c
As the preceding example implies, you can run all your command -line
executables from within the PowerShell console; you can even run VBScript
scripts and batch files from within the PowerShell console. Want to run the
script C:\Scripts\Test.vbs from within Windows PowerShell? Okey-doke:
c
c
/#
c
c012c
c c3
c4
c,( c
. c02c
c
c%&&( '**%cc
c
/c
5
c$
c
c
c#.c5
/#
c
ü . Interested in learning more about the console itself? Then take a look at the
and on
!.
Oh, and don¶t forget tab expansion.
In addition, a lot of the console commands you¶ve grown to love -
like and , or even nix commands like - have Windows PowerShell
equivalents. Although there are some minor exceptions, these commands
work exactly the way you would expect them to work. Want to change from
the C:\Scripts folder to the C:\Windows folder? Then just use the cd
command:
c
cc
c
c!6ccc
67cc.c c
c
cc
c
c!6c
-
c
In case you¶re wondering, PowerShell commands are typically case -
insensitive. You could also type from the command prompt:
c
Or even this:
5 c
Top of page
Oh, right; good question. Cmdlets are Windows PowerShell commands, and
are roughly equivalent to command-line tools. (Although, in this case,
cmdlets are PowerShell-specific; you can¶t run a PowerShell cmdlet unless
you are running Windows PowerShell). Without command -line tools
Cmd.exe wouldn¶t be of much use. Although the console has a few built -in
commands (such as cd and cls) most of the ³heavy lifting´ (and most of the
useful tasks) are carried out by command-line tools such as Ipconfig.exe
and Ping.exe. Windows PowerShell works in a similar way: although the
shell has a few built-in commands, most of the heavy lifting is performed by
cmdlets.
c
c
c
c
c!6c
c!6c-
You pm get back information similar to this, information about all the
processes currently running on your computer:
3
cc 082cccc 082cccccc082c402ccc 90
2cccccc
$c
cc cccc cccccc c ccc ccccc c
c
cccc%*:ccccccc,ccccc%'&(ccccccc:(+(cccc:'ccccc**,ccc'&(;c c
cccc'(;ccccccc+ccccc;)+'cccccc%*';;cccc+*ccccc*);cccc&'*c
c
cccc%*%ccccccc;ccccc:*)*ccccccc;(&'cccc:)ccccc%++ccc'%';c
""c
cccc%()ccccccc+ccccc,,);ccccccc(&()cccc,;ccccc*:*ccc')&(c
<5 /c
cccc%;:ccccccc,ccccc:+%'ccccccc((;*cccc,'ccccc*:*c cc'++(c<55.c
cccc+,'cccccc%:cccc%%*;)cccccc'*',(cccc++ccccc')%cccc;,'c
$"c
cccc(+'ccccccc+ccccc'+&(ccccccc()(;cccc()cccc%))*ccc%%);c
cccc%:&ccccccc(ccccc%'*;ccccccc;,;;cccc:)ccccc*+*ccc'+(*c $ c
Pretty slick, huh? Maybe you¶d like to list all the files and folders in
C:\Scripts, including anything which happens to live in a subfolder of
C:\Scripts. Well, all you had to do was ask:
- $c
c
c
Now that¶s all well and good, but the Scripting Son didn¶t really learn how to
drive until he got behind the wheel and started driving: reading about
something is no substitute for actually m something. Windows
PowerShell is no different: you can read about cmdlets all you want, but
you won¶t get a good feeling for cmdlets and wh at they can do until you get
behind the wheel and actually start using Windows PowerShell. And while
the Scripting îuys can¶t sit in the passenger¶s seat and offer useful advice
(like, ³Slow down! Watch out for the curb! Don¶t you slam on the
brakes like that!) we
do the next best thing: we can provide step -by-
step guidance that walks you through a typical system administration
scenario using Windows PowerShell. If that sounds like fun (and why
wouldn¶t it be?), take a look at our
)
.
*
Here¶s a little time-saving tip for you. When using parameters in conjunction
with a cmdlet, you only have to type as much of the parameter name as
needed to make that parameter distinct. What does that mean? Well, îet -
ChildItem has only one parameter (recurse) that starts with the letter .
That means we can specify the -recurse parameter by using this command:
- $c
c c
- $c
c c
Why did we have to type -rec? That¶s easy: we had to type three letters
before we could differentiate -recurse and -readonly:
|c -reurse
|c -redonly
Still too much typing for you? Well, if you use the alias (which, needless
to say, is the alias for îet-ChildItem) you can retrieve a recursive list of all
the files and folders in C:\Scripts by doing no more typing than this:
c
c c
Cool.
Top of page
Yes, we know: you¶d like to know what other cmdlets are available to
you m you¶d like to know what you can do with all those cmdlets. To tell
you the truth, we won¶t be able to answer those questions in this article.
However, we
point you to readily-available resources that will help you
answer these questions for yourself.
To begin with, Windows PowerShell is a p retty chatty and out-going
technology: all you have to do is ask and PowerShell will be happy to tell
you all about itself. For example, remember the îet -Alias cmdlet, which
returned a list of all the PowerShell aliases available to you? Well, there¶s a
similar cmdlet -
&$ - that returns a list of all the PowerShell
cmdlets available to you. Here¶s a partial look at what happens when you
run îet-Command:
c
c- $$
c
$$
5.ccccc$cccccccccccccccccccccccccccccccccccccccccccccc cc
!
c
ccccc cccccccccccccccccccccccccccccccccccccccccccccccc
c
$ cccccccccc 3
.ccccccccccccccccc cccccccccccccccccccccccc
3
.c>>
6#A ?c@ 6#A >??c>
c
5?c> 4c
, ë
What do the three dots («) at the end of each line mean? They mean that
there is additional information about each cmdlet; however, that
information wouldn¶t all fit on the screen. Is there a way to it all fit on
the screen? Sure; all you have to do is display the information in list view
rather than table view:
c
c- $$
cCcB$
c
$cccccccccccccc
c
$$
5.ccccccc$ c
ccccccccccccccccccc
$?c>
c
@B.
$$ /
? c
ccccccccccccc c
$#. ccccc c
!ccccccccccccccc
!6
$#.-D
$$
$
%***DD:%#:),(:(;:, c
ccccccccccccccccccc
$$
$
c
3Bcccccccccc
$$
$
3"$c
$
cccccE 7c F c
$$
5.cc
$$
$$
c
4#cccccccccccccc c
cccccccccccccc c
îet-Command is pretty useful: after all, it gives you a definitive list of the
cmdlets that are available to you. However, it doesn¶t tell you how
to p those cmdlets. For that, you need to access PowerShell¶s built -in help
system.
Top of page
One of the really nice features of Windows PowerShell is the fact that it
includes a very extensive help system, which just happens to be
incorporated right into the shell itself. Once you know the name of a cmdlet,
you can get additional information on that cmdlet (and how to use it) by
calling the
&+ cmdlet:
- 3cB$
c
Wait, wait: don¶t type that in yet. Like we said, PowerShell has a very
extensive help system; however, it also has a somewhat
m help
system. Typing
&+ -
&' will, indeed, show you additional
information about the Format -List cmdlet; however, that information will
consist largely of a syntax diagram and a brief description of the cmdlet. For
more extension information, including example commands and a detailed
description of allowed parameters, you need to tack on the & parameter,
like so:
- 3cB$
c c
- 3cB$
c "$
c
Wait, wait: don¶t type those commands in yet, either. The actual help topics
found in PowerShell are very, very good; however, PowerShell¶s help engine
leaves a little something to be desired. For example, when you open a help
topic using îet-Help, PowerShell displays the entire topic and then leaves
the cursor at the very end of the topic. To actually read the information
from start to finish you have to scroll up and find the first line. You can
work around that problem (sort of) by p iping the results of îet-Help to
the
command, which enables you to display information one page
(screen) at a time:
- 3cB$
c cCc$c
That helps, although you can only page your way forward; you can¶t go
backwards. (Try it and you¶ll see what we mean.)
The other issue is a little harder to work around: for better or worse (mostly
for worse) PowerShell¶s help engine doesn¶t do a very good job of
respecting word breaks at the end of a line. For example:
!5!c!1 56 c
cccc5c-
c#A ccc
c c$
7c=-
=c
ccc c
ccccc
c
c c$ 7c
c c.c .c=-
c#.c c
cccc
c $cc
cc
c#A c
c c
c c-
cBc-
7c c
c $cBc
7c c
c$ c
c#.c
c! c
- -93cB$
c
And no need for parameters like -full or -examples. With the graphical help
file you always get the information available for a topic.
But you know what? You¶re right. Help is incredibly useful, provided that
you already know what it is you want to do. (For example, you know you
want to use îet-ChildItem to list all the files and folders found in C: \Scripts,
you just can¶t remember which parameter enables you to recursively search
for these files.) But what if you have no idea what you can even m with
Windows PowerShell in the first place? Are you just supposed to read the
help file from start to finish?
Well, you can if you want to. Alternatively, you might want to take a look at
the Script Center¶s &* (
.
Here you¶ll find scores of task-based articles, with titles like ) ë
- and $!-
-
. One thing you might want to do is
browse through those articles and look in the help file for more
detailed information on the cmdlets used to carry out those tasks.
ü . And yes, the graphical help file includes links to corresponding articles in the m
information about the Set-Content cmdlet in the graphical help file you¶ll see a link to the article
Top of page
You don¶t think we¶d forget about scripts and scripting, do you? (They don¶t
call us the
îuys for nothing.) Windows PowerShell provides a very
cool and very powerful environment for writing and running scripts;
however, actually getting scripts to run unde r PowerShell can be a little
tricky, at least at first. Therefore, this Owner¶s Manual includes a
on running scripts in Windows PowerShell.
ü . OK, there might be a more: you actually have to write some script code. Yet anothe
.
Here¶s another cool thing about PowerShell. Throughout this article we¶ve
referred to PowerShell as being a com mand console and scripting language.
There¶s a good reason for that: you can run PowerShell commands from the
command prompt you can save those commands as .ps1 files and run
them as scripts. Even cooler, these commands can be (and usually are)
identical: the commands you type at the command prompt can be used as -
is in scripts, and vice-versa.
Ic
In the first line, this script uses îet-ChildItem to retrieve a collection of all
the files and folders stored in C: \Scripts; that collection is stored in the
variable $a. In the second line, the script simply displays the value of $a.
Not all that exciting, is it? That¶s hard to argue with. However,
what exciting is this: you can either save these two commands as a .ps1
file and then run those commands as a script, or you can type the
commands at the command prompt, one after another. What happens if you
type the commands at the command prompt? You got: the first co mmand
retrieves a collection of all the files and folders stored in C: \Scripts and
stores that information in a variable named $a; the second command
echoes back the value of $a. Work from the command prompt or write a
script? In a certain sense, it doesn¶t really matter.
Needless to say, we¶ll have more - much more - about scripts and script
writing in future updates to the p.
Top of page
ë(
-
+
%
We¶d suggest two things. First, take a look at the
, five presentations introducing Windows PowerShell. Those
webcasts include:
|c Amazing But True: Thing You Never Dreamt You Could Do with
Windows PowerShell
Second, kick the metaphorical PowerShell tires by trying out the )
. It¶s always better to try something hands-on rather than read about it.
(Which, you might have noticed, we didn¶t bother to tell you until you¶d
finished reading this big, long article.)
And keep visiting the Script Center¶s
. We¶ll be adding
more and more content to this section, including more installments to
the p.