Executive Summary:
Windows PowerShell offers many different types of operators. For example, its comparison and logical operators let you create conditions that PowerShell evaluates to determine whether it should take specific actions. And its arithmetic operators let you not only perform mathematical calculations but also concatenate strings. |
In PowerShell, you can connect cmdlets together to create a
pipeline and use the Where-Object cmdlet to filter objects
passed down that pipeline. For example, in the following
statement, objects from the Get-ChildItem (referenced by
the dir alias) cmdlet are piped to a Where-Object cmdlet
(referenced by the where alias), which filters out all items
in C:\Windows, except those larger than 500,000 bytes:
dir c:\windows |
where {$_.length -gt 500000}
Notice that the Where-Object cmdlet includes an expression
that’s enclosed in braces ({ }). The expression states that the current
value of the Length property must be greater than 500,000. The value
of the Length property is retrieved by using $_.length. The $_ symbol
references the current object in the pipeline, and .length retrieves the
value of the Length property. The expression then uses the -gt (greater
than) operator to compare the Length property value to the value of
500,000.
As with any language, PowerShell provides a set of operators
that let you create expressions you can incorporate into your statements.
An expression is a block of code that PowerShell evaluates;
the result of that evaluation determines what action to take. For
example, in the preceding statement, PowerShell determines
whether the Where-Object expression is true or false. When the
expression evaluates to true—that is, the current object’s Length
property value is greater than 500,000—that object is passed down
the pipeline and displayed in the output. If the expression evaluates
to false—that is, the current object’s Length property value isn’t
greater than 500,000—the object is discarded and not displayed in
the output.
PowerShell includes a variety of operators that you can use in
your expressions. This lesson describes many of those operators and
provides examples of how to use them. In addition, the Web-exclusive
sidebar “How to Find Out a Cmdlet’s Properties,” www.windowsitpro.com, InstantDoc ID 98175, discusses how to find property names,
such as those used in the Where-Object expressions in the examples
provided.
Comparison Operators
As the name suggests, comparison operators compare values. When
an expression contains a comparison operator, PowerShell compares
the value to the left of the operator with the value to the right of it. You
saw this idea in the preceding example, in which the Length property
value is compared to 500,000. PowerShell provides many comparison
operators, as Table 1, shows. Let’s examine some of those
operators to see how they work.
The following statement does the opposite of the preceding example—
it returns items whose lengths are smaller than 500,000 bytes:
dir c:\windows |
where {$_.length -lt 500000}
As you can see, the only difference between
the two statements is the comparison operator.
This statement uses the -lt (less than)
operator rather than the -gt operator.
Other comparison operators follow the
same logic. The following statement uses
the -eq (equal to) operator to compare the
Responding property value to the string true in order to retrieve a list of responding processes:
get-process |
where {$_.responding -eq “true”}
For the Where-Object expression to evaluate
to true, the Responding property value must
equal true. As a result, only responding processes
are returned, as Figure 1 shows.
By default, all comparison operators
perform case-insensitive comparisons. If
you want to be more precise in your code,
you can add the letter i to a comparison
operator (e.g., -ieq) to explicitly specify
a case-insensitive comparison. However,
because this is the default behavior, adding
the i isn’t necessary.
You can make any comparison casesensitive
by adding the letter c to the comparison
operator (e.g., -ceq). For example, the
statement
“True” -eq “true”
evaluates to true because it ignores case,
whereas the statement
“True” -ceq “true”
evaluates to false because it takes case into
account.
I realize that these are very basic examples,
but when working in a Windows environment,
case often isn’t a concern because
filenames, process names, and other item
names are case-insensitive. But as you
become more familiar with PowerShell and
learn how to retrieve other types of lists in
which case-sensitivity is important, you’ll
find being able to make an operator casesensitive
useful.
Another useful PowerShell feature is wildcards.
For example, if you don’t know the
exact name of an item when creating an
expression to compare values, you can use
wildcards in the compared value (the value
after the operator). Table 2 describes the wildcards that PowerShell supports.
Continued on page 2