You implement wildcards through the
use of the -like and -notlike comparison
operators. (Note that -like and -notlike as well as -match, -notmatch, and -replace are
sometimes referred to as pattern-matching
operators.) For instance, suppose you want
to find all Google-related processes on a computer. You can use the -like operator to
return all processes created by companies
whose name includes the string google:
get-process |
where {$_.company -like “*google*”}
The asterisk wildcard matches zero or more
characters, so you’ll receive accurate results
no matter whether the company name is
stored in Windows as Google, Google Inc.,
or another variation. Figure 2 shows the
results from this statement. If you were to
use the -notlike operator instead of the -like
operator, all non-Google processes would be
returned.
In addition to wildcards, PowerShell supports
regular expressions, which are based
on the Microsoft .NET Framework regular
expression classes. You implement regular
expressions through the use of the -match
and -notmatch operators. PowerShell’s support for regular expressions is quite extensive—
as extensive as you would find in any
.NET language. For this reason, a discussion
about them is beyond the scope of this lesson.
For information about them, see PowerShell’s
about_regular_expression and about_comparison_
operators Help files.
Logical Operators
So far, I’ve discussed how to use comparison
operators in expressions. When you use one
of these operators, you create a condition
that’s evaluated to determine whether to take
a specific action. However, in some cases,
you might want to create expressions that
include multiple conditions. In other words,
you might want to perform more than one
comparison to determine whether to take
that action.
To perform multiple comparisons in
a single expression, you must use logical
operators to link conditions together. Logical
operators, which are described in Table 3, specify what logic to use when
evaluating multiple conditions.
Let’s take a look at an example to
illustrate how logical operators work.
The following statement uses the Get-
Process cmdlet to retrieve a list of
running processes:
Get-Process |
where {($_.handles -gt 500) `
-and ($_.pm -ne 0)}
There are two conditions, each of which is
enclosed in parentheses. The first condition
($_.handles -gt 500) specifies that the number
of handles must be greater than 500 for a
given process. The second condition ($_.pm
-ne 0) specifies that the paged memory size
must not equal 0. The -and logical operator
connects these two conditions. As a result,
both conditions must evaluate to true for
the entire expression (enclosed in braces)
to evaluate to true. Only those processes that
meet both of these conditions are returned,
as Figure 3 shows.
Now let’s take a look at the -or operator.
The following statement is the same as the
preceding example except that it uses -or instead of -and:
get-process |
where {($_.handles -gt 500) `
-or ($_.pm -ne 0)}
In this case, at least one of the conditions
must evaluate to true for a process to be
included. In other words, the process must
have a handle count greater than 500 or
the paged memory size must not equal 0 or
both. As a result, many more processes are
returned, as Figure 4 shows.
You can use the -not logical operator to
indicate that a specified condition must not
be true. For example, the following statement
specifies that the handle count must
be greater than 100 and the company name
must not be Microsoft Corporation:
get-process |
where {($_.handles -gt 100) `
-and -not ($_.company -eq `
“Microsoft Corporation”)}
This statement returns all non-Microsoft
processes, as Figure 5 shows.
Continued on page 3