Datasheet

Chapter 1: Getting Started with Windows PowerShell
25
The value to test must be of the same data type of the property being tested. For example, if the property
data type is string, a string value enclosed in quotes must be used for the test. If the property data type is
integer, a numeric value must be used for the test and so on.
While Windows PowerShell validates the syntax used inside the script block, the result of entering an
unknown property name or an invalid data type is a failure to pass any objects down the pipeline
without reporting any error to the operator.
Multiple test conditions can be used in the same script block as long as they are separated by one or
more conjunctive or disjunctive operators:
The
and conjunction operator is used to compare the Boolean results of two or more test condi-
tions to render a concluding Boolean value. If any of the test conditions are true, a
true is
returned.
The
or disjunctive operator is used to compare the Boolean results of only two test conditions. If
either one or both test conditions are false, a
false is returned.
Conjunctive and disjunctive groups of test conditions can be used in the same script block as
long as they are enclosed in parentheses.
Now let s look at a practical example of using
Where-Object as a filter in the pipeline stream. Say that
you need to change the
Manager property for several users based on the department to which they
belong (Engineering), and the office from which they work (the Dallas office) to show they report to
manager John Doe. The command would look like this:
[PS] C:\ > Get-User | Where-Object { $_.Office -eq “Dallas” -and $_.Department -eq
“Engineering” } | Set-User -Manager “John Doe”
After collecting all users with Get-User , the collection of objects is passed to Where-Object to apply
the test conditions on each object one at a time. The first test checks for the Office property equal to
“Dallas” . The second condition checks the Department property equal to “Engineering” . If both test
conditions result in true, the object is passed to the next command. If one of the test conditions results in
false, the object is disposed. After processing all objects in the stream, they are passed to
Set-User for
applying the modification.
Finding Property Names and Data Types
To use Where-Object effectively you need to know the available property names and data types for
the objects being passed by a given cmdlet. By passing the results of any cmdlet that uses the
Get verb
to the
Get-Member cmdlet you can generate a list of properties and their data type. For example,
the command in Figure 1 - 18 sends the objects collected by the
Get-Mailbox cmdlet to Get-Member and
displays the objects properties and their definition.
It is important to know the exact name of each property used in the
Where-Object script block.
Mistyping a property name results in a failure to pass any objects down the pipeline stream without
reporting any error to the operator.
c01.indd 25c01.indd 25 12/17/07 3:19:30 PM12/17/07 3:19:30 PM