Executive Summary:
Windows PowerShell provides far more power and flexibility than traditional Windows command shells, such as cmd.exe. To help you take advantage of that power, this six-article series explains how to use PowerShell to perform various tasks. In this article, you'll learn how to start using PowerShell and how to run basic PowerShell commands. You'll also learn how to get help within PowerShell when creating those commands and how to use aliases in your commands. |
By now, most administrators
are familiar with
Windows PowerShell.
Many have downloaded
it, played with
it, and perhaps used
it to perform ad hoc
tasks as they might do
in Windows command
shell (cmd.exe). But PowerShell is a lot more
than a simple DOS-like command shell. It’s a
command-line and scripting environment that
leverages the Microsoft .NET Common Language
Runtime (CLR) and .NET Framework.
When you work in the PowerShell environment,
you’re working with .NET objects. The
folder structures you view or the services you
access are actually instances of objects that
represent the folders and services, unlike other
command shells that simply process text. As a
result, PowerShell provides far more power and
flexibility than traditional command shells.
To help you take advantage of this power,
I’m writing a series of articles that explain how
to use PowerShell to perform various tasks.
Each article is a lesson that builds on previous
lessons in order to demonstrate important
PowerShell concepts. In this lesson, I explain
how to get started using PowerShell and how
to run basic PowerShell commands. I also
show you how to get help within PowerShell
when creating those commands and how to
use aliases in your commands.
Getting Started
PowerShell currently doesn’t ship with Windows
OSs, although this will change starting
with Windows Server 2008. You can find Power-
Shell download links and information on the
Windows PowerShell Web page (www.microsoft.com/powershell). Before you download PowerShell,
install .NET Framework 2.0 if you don’t
already have it. The PowerShell installation process is quick and straightforward. Just be
sure to install the PowerShell edition specific to
your OS. Microsoft provides editions for Server
2008 beta 3, Windows Vista, Windows XP SP2,
and Windows Server 2003. For this article, I’m
running PowerShell on XP.
After PowerShell is installed, you can use it
immediately. To run PowerShell, select All Programs
under the Start menu, choose Windows
PowerShell 1.0, and click Windows Power-
Shell. When the PowerShell window appears,
the command prompt displays the current
working folder (C, on my system). You’re now
ready to start writing and executing PowerShell
commands.
Working with Cmdlets
PowerShell supports its own scripting language,
which is based on the .NET Framework.
The most basic command in that language
is the cmdlet (pronounced command-let).
A cmdlet is similar to a function in that it
performs a specific task, such as retrieving a
folder’s contents or updating a registry entry.
PowerShell includes more than 100 built-in
cmdlets. You can create additional cmdlets,
but you must create them in a .NET language,
such as Visual Basic .NET or C#. (The Power-
Shell 101 series will discuss only the built-in
cmdlets.) Each cmdlet is in the form verb-noun
because Microsoft wanted to use a consistent
naming scheme to make PowerShell easy
to learn and expand. The verb specifies the
action to be taken. The noun indicates the
type of object involved. For example, the Get-ChildItem cmdlet retrieves a list of items in the
current working directory or container, such
as the registry. To run the cmdlet, type it at the
PowerShell command prompt and press Enter.
The results are displayed beneath the command
prompt. That’s all there is to running a
basic command.
There will probably be times when you
don’t know whether there’s a cmdlet for the
task you need to accomplish or when you can’t
remember a cmdlet’s name. You can view a
list of all cmdlets by using the Get-Command
cmdlet. Figure 1 shows part of this list, which
includes the cmdlets’ names and syntax, but
not a description of what the cmdlet does. To
get that information, you can use the Get-Help
cmdlet.
Getting Help with Cmdlets
PowerShell includes a set of Help files that
you can access directly from the Power-
Shell command window with the Get-Help
cmdlet. To retrieve Help information about
a specific cmdlet, you use Get-Help with
its -name parameter followed by the name
of the cmdlet you want to learn about. Like
parameters in cmd.exe commands, parameters
in PowerShell cmdlets provide information
that the cmdlets need to do their job.
Unlike parameters in cmd.exe commands
(which might start with a hyphen, a slash, or
no symbol at all), parameters in PowerShell
cmdlets always begin with a hyphen, which
is another example of PowerShell’s consistent
naming scheme.
Now let’s take a look at an example to demonstrate
how this works. A common system
administrator’s task is to read text files. After
looking at the list of cmdlets that Get-Command
provided, you think the Get-Content
cmdlet might do the trick but you aren’t sure.
To retrieve Help information about Get-Content,
run the command
Get-Help -name Get-Content
As Figure 2 shows, this command returns a
description of the cmdlet and syntax information.
The command returns the content of an
item, which in this case refers to any type of file
in a system. In the past, you might have used
the For command for batch files or the File-
SystemObject object in a Windows Script Host
(WSH) script, but in PowerShell, you simply
use the Get-Content cmdlet. You can retrieve more detailed information about the syntax by
adding the -full parameter to the command
Get-Help -name Get-Content -full
Notice that the -full parameter doesn’t take a
corresponding value. This type of parameter is
called a switch parameter because it switches
the behavior of the cmdlet.
Figure 3 shows some of the information
returned by this command. (On your computer,
you’ll need to scroll or resize your
window as necessary to view the entire contents.)
The PARAMETERS section provides the
information you need to include parameters in
your command. Two important categories of
information for each parameter are Required
and Position.
The Required category tells you whether
the parameter is mandatory or optional. When
Required is set to true, you must include the
parameter. When Required is set to false, the
parameter is optional.
The Position category tells you whether a
parameter must be named or whether it can be
referenced by its position. When Position is set
to named, you must include the parameter’s
name when referencing that parameter. When
Position is set to a number, you can reference
the parameter by its name or you can simply
provide the parameter’s value in its correct
position.
For example, as you can see in Figure 3, the
-path parameter is required for Get-Content.
However, you can include that parameter
value in the first position without including the
parameter name, as in
Get-Content c:\sample.txt
If a parameter value contains spaces, you must
enclose the value in quotes.
In the PARAMETERS section, each parameter
name is followed by information in angle
brackets (< >). This information specifies the
type of data that the parameter value must be.
As Figure 3 shows, the -path parameter value
must be a string. If a set of brackets ([ ]) follow
the word string, then a string array is permitted
as the parameter value.
In the case of switch parameters, which
don’t take values, the data type will read
. For example, Get-Content’s
-force parameter is defined with this data
type. This parameter overrides restrictions that
might prevent the command from succeeding. The override occurs only when you include the
parameter in your command.
One other feature to note about parameters
is that PowerShell includes a parameter-name
completion feature. You need to include only
enough of the parameter name to distinguish
it from other parameters. For example, the
command
Get-Content c:\sample.txt -force
is the same as
Get-Content c:\sample.txt -fo
Besides providing the
parameter information that
you need to build commands,
the Help file for Get-Content
includes examples of how to
use the cmdlet, helpful tips
in the Notes section, and
resources where you find additional
information. The best
part is that Help files are available
for all the cmdlets—there
are even Help files that discuss
general concepts.
Getting
Help with
Concepts
PowerShell includes a set of
Help files that provide overviews
of various concepts.
Each file begins with “about_”
and ends with the name of the
topic. To view an alphabetical
list of the about topics, run the
command
Get-Help about*
To view information about
a specific topic, you simply
include the topic’s full name
as a parameter value. For
example, to retrieve the file
about flow control, run the
command
Get-Help about_flow_control
Figure 4, shows part
of the results you can expect. As you can see, the file provides an overview
of how to implement flow control in a Power-
Shell script.
Using Aliases
Some of the cmdlet names can be quite verbose,
an annoying characteristic if you have to
continuously retype commands. Fortunately,
PowerShell supports the use of aliases for
referencing cmdlets. An alias is an alternate name that’s usually much shorter than the
actual cmdlet name. PowerShell includes a
number of built-in aliases, and you can create
your own aliases.
To view the aliases available to your current
session, run the Get-Alias cmdlet. Current
session refers to your current connection to
PowerShell. When you start PowerShell, you
start a new session; that session persists until
you close PowerShell, which ends your connection.
In addition to displaying all built-in
aliases and their associated cmdlets, Get-Alias
displays any aliases you created in the current
session and aliases defined in profiles, which
are user-defined configuration settings loaded
into PowerShell whenever it starts. (Profiles
will be discussed in a later lesson.)
If you want to view the aliases available for
a specific cmdlet, you must qualify the Get-
Alias cmdlet. For example, to view the aliases
available to the Get-ChildItem cmdlet, run the
command
Get-Alias |
Where-Object {$_.definition `
-match “Get-ChildItem”}
This command incorporates several elements
that I’ll explain in detail in subsequent lessons.
For now, all you need to know is that the results
of the Get-Alias cmdlet are sent to a Where-Object
cmdlet that filters out any results that don’t
match Get-ChildItem. If you want to check for
aliases for a different cmdlet,
replace Get-ChildItem with
the cmdlet name.
As you can see in Figure
5, PowerShell includes three
aliases that reference Get-
ChildItem: gci, ls, and dir.
You can use any of these
aliases in place of the cmdlet
name. All four of the following
commands list the
contents of the C:\Windows
folder:
Get-ChildItem c:\windows
dir c:\windows
ls c:\windows
gci c:\windows
To create an alias within
the current session, use the
Set-Alias cmdlet. For instance,
to create an alias named cnt
that references Get-Content, run the command
Set-Alias cnt Get-Content
You can then use cnt wherever you would
use Get-Content. The alias is available until
you end your session (i.e., close PowerShell).
Note that you can’t include parameters when
defining an alias, only the cmdlet name itself.
If you want to define a reference to a cmdlet
and its parameters, you should create a function.
You’ll learn how to create a function in a
later lesson.
Moving Forward
In this lesson, I introduced you to the fundamental
components necessary to begin
exploring and using PowerShell commands,
which consist of one or more cmdlets. In
upcoming lessons, you’ll learn more about
how to use these cmdlets and how to create
scripts that enable you to leverage Power-
Shell’s full capabilities. In the meantime,
begin working with cmdlets. Use Power-
Shell’s Help file to create commands and
learn about specific concepts. Try out the
different parameters and learn how to create
and use aliases. In no time at all, you’ll be
ready to incorporate PowerShell into your
daily routines.