Filtering techniques Where-Object

Filtering techniques with Where-Object

The key to making the most of Windows PowerShell is understanding how objects and the pipeline work together to provide exactly the information you need. Each command in the pipeline generates one or more objects and passes them to the next command. A crucial part of this process is the ability to filter those objects and their data as they move from one command to the next. In this section, I’ll discuss Filtering techniques with Where-Object.

PowerShell includes a number of cmdlets that allow you to build commands that refine the output of your pipeline.

Where-Object

Let me start with an example. It includes the following values.

Filtering techniques with Where-Object
Get-Module | Where-Object {$_.Version -eq '2.4.0'}

The script consists of three components: the property you’re filtering on (Version), a comparison operator (-eq), and the value you’re filtering against (2.4.0).

The output is as follows:

Filtering techniques with Where-Object

We can see here that the full output is not readable. A better approach would be:

Get-Module | Where-Object {$_.Version -eq '2.4.0'} | Format-Table -AutoSize
Get-Module | Where-Object {$_.Version -eq '2.4.0'} | Format-List

When you reference a property in a Where-Object script block, you must precede the property name with the $ symbol, followed by a dot. The $ symbol represents the current object in the pipeline. Since the Where-Object command is applied to each object as it flows through the pipeline, the $_ symbol allows you to reference the specified property on each object without needing to know how many objects are being passed or distinguish between them.

Other Filtering Techniques with Where-Object

The next element is the comparison operator. PowerShell includes several:

  • -ne (not equal to)
  • -lt (less than)
  • -le (less than or equal to)
  • -gt (greater than)
  • -ge (greater than or equal to)
  • -like (wildcard comparison)
  • -notlike (negated wildcard comparison)
  • -contains (contains the specified value)
  • -notcontains (does not contain the specified value)

The Where-Object script block also allows you to use logical operators to combine multiple expressions. This lets you evaluate several conditions within a single script block. The logical operator determines how the individual expressions are handled to produce a final evaluation. If the entire script block evaluates to True, the object is included in the results. PowerShell supports several logical operators:

  • -and (the script block evaluates to True if both expressions evaluate to True)
  • -or (the script block evaluates to True if at least one expression evaluates to True)
  • -xor (the script block evaluates to True if one expression is True and the other is False)
  • -not or ! (negates or reverses the truth value of the following expression)

Here are a few more examples.

Get-Module | Where-Object {($_.Version -eq '7.0.0.0') -and ($_.Name -like '*Utility')}

The output is now as follows.

Filtering techniques with Where-Object

Here is another example.

Get-Module | Where-Object {($_.Version -eq '7.0.0.0') -or ($_.Name -like '*Utility')}
Output
Get-Module | Where-Object {($_.Version -eq '7.0.0.0') -xor ($_.Name -like '*Utility')}

The output would then be as follows:

Output

Meer informatie over PowerShell vind je hier. Meer informatie over de auteur van deze blog post vind je hier.

0 Shares:
You May Also Like