PowerShell – Where-Object with Multiple Conditions

PowerShell- Where-Object with multiple conditions

Using Where-Object Cmdlet with Multiple Conditions

In PowerShell, we have various comparison and logical operators that we can use with the Where-Object cmdlet to apply multiple conditions. Let’s explore a few scenarios below.

The above command returned an item where the extension of the selected item is .txt, and its length is equal to 42. Now, how is this command working? It is essential to learn to move ahead in this article.

The Get-ChildItem cmdlet retrieves the items and child items in one or multiple locations we specified (if not specified, it will use the current directory). On the other hand, the Where-Object cmdlet chooses objects from the collection based on their property values that are passed to it.

For instance, we can use this cmdlet to choose files with a specific size or extension, just like we did in the above example code. The point is how we can construct the Where-Object command.

There are two ways of constructing it that are given below. Note that we can only use any of these approaches if our PowerShell version is 3.0 or greater (to check the version of your PowerShell, open up a PowerShell console (or the ISE), type $PSVersionTable and hit Enter. Now, look for the value of PSVersion that is your PowerShell version).

Script Block

These are the virtual components in Windows PowerShell, used in multiple places within the scripting language. It is an anonymous function which can categorize code and runs it in different places. For example, we can use a script block to specify a comparison operator, property name, and property value.

The Where-Object will return all objects for which a script block statement is True. So, for instance, the following command gets that item whose name is File1.txt and length is 42.

In PowerShell, we can pipe the objects to the Where-Object and use the FilterScript parameter since it accepts a script block, so we can create single or multiple conditions to assess the property of every object is equal to a particular value or not. See the following example to understand.

Note that we can also omit the -FilterScript parameter name to write scripts faster and more cleanly, but this makes it hard to understand for Windows PowerShell beginners, specifically when we will have multiple conditions.

One important thing while using script block with multiple conditions is to wrap around your subexpression with () in a filtering syntax. Why? It is because by wrapping our comparisons in {}, we are creating script blocks, so a PowerShell interpreter views it as Where-Object { <ScriptBlock> -and <ScriptBlock>}.

Since the -and operator performs its operator on Boolean values, PowerShell casts the <ScriptBlock> to Boolean values. Remember, in PowerShell, everything excluding not empty, null, and zero is True. So, the following statement:

will look like as follows, which will always be True:

This is why we use parentheses () instead of {} for sub-expression. See the following example:

Comparison Statements

We can also use comparison statements with Where-Object where we are getting processes using Get-Process that has the priority class of Normal. We can use both statements interchangeably.

Remember that comparison statements are supported if you have PowerShell version 3.0 or greater.

Now, let’s compare a statement using script block and comparison statement.

Remember that we use comparison statements when we have only one condition. Why is it so? When we use Where-Object without a script block, the comparison operators behave like parameters, not operators.

So, we can not use multiple comparison parameters. This is why we can only use multiple comparisons in a script block. Let’s see another example of Where-Object with multiple conditions where we find all the processes that use between 4% & 8% of the CPU.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *