PowerShell Where-Object
The Where-Object in PowerShell selects the objects from a collection based on their property values.
With the Starting of PowerShell version 3.0, you can construct the Where-Object cmdlet in the following two different ways:
- Comparison Statement: With the Starting of PowerShell 3.0, we can add the comparison operators as the parameters in the Where-Object command.
- Script Block: We can use the script block to specify the name and value of the property and a comparison operator. The Where-Object cmdlet returns all the objects for which the statement of script block is true.
Syntax
Parameters
Following are the parameters used in this cmdlet:
-Property
This parameter is used to specify the name of an object property. It is optional and was introduced in the 3.0 version of Windows PowerShell.
-Value
This parameter is used to specify the value of a property. It accepts the wildcard (*) characters when used with the comparison parameters. It was also introduced in version 3.0 of PowerShell.
-InputObject
This parameter is used to specify those objects which are to be filtered. We can also pipe the objects to the Where-Object cmdlet.
-EQ
The Where-Object cmdlet used this parameter to get the objects if the value of the property is the same as the specified value.
-FilterScript
This parameter is used to specify the block of a script for filtering the objects. You must enclose the script block in the braces {}.
-Match
The Where-Object cmdlet used this parameter to get the objects if the value of the property matches the given regular expression.
-CEQ
The Where-Object cmdlet used this parameter to get the objects if the value of the property is the same as the specified value.
-NE
The Where-Object cmdlet used this parameter to get the objects if the value of the property is different from the specified value.
-CNE
The Where-Object cmdlet used this parameter to get the objects if the value of the property is different from the specified value. This operation is case-sensitive.
-GT
The Where-Object cmdlet used this parameter to get the objects if the value of the property is greater than the given value.
-CGT
The Where-Object cmdlet used this parameter to get the objects if the value of the property is greater than the given value. This operation is case-sensitive.
-LT
The Where-Object cmdlet used this parameter to get the objects if the value of the property is less than the given value.
-CLT
The Where-Object cmdlet used this parameter to get the objects if the value of the property is less than the given value. This operation is case-sensitive.
-GE
The Where-Object cmdlet used this parameter to get the objects if the value of the property is greater than or equal to the given value.
-CGE
The Where-Object cmdlet used this parameter to get the objects if the value of the property is greater than or equal to the given value. This operation is case-sensitive.
-LE
The Where-Object cmdlet used this parameter to get the objects if the value of the property is less than or equal to the given value.
-CLE
The Where-Object cmdlet used this parameter to get the objects if the value of the property is less than or equal to the given value. This operation is case-sensitive.
-Like
The Where-Object cmdlet used this parameter to get the objects if the value of the property matches a value which includes the wildcard characters.
-CLike
The Where-Object cmdlet used this parameter to get the objects if the value of the property matches a value which includes the wildcard characters. This operation is case-sensitive.
-NotLike
The Where-Object cmdlet used this parameter to get the objects if the value of the property does not match a value, which includes the wildcard characters.
-CNotLike
The Where-Object cmdlet used this parameter to get the objects if the value of the property does not match a value, which includes the wildcard characters. This operation is case-sensitive.
-CMatch
The Where-Object cmdlet used this parameter to get the objects if the value of the property matches the given regular expression. This operation is case-sensitive
-NotMatch
The Where-Object cmdlet used this parameter to get the objects if the value of the property does not match the given regular expression. If the input is scalar, the value which is matched is saved or stored in the $Matches automatic variable.
-CNotMatch
The Where-Object cmdlet used this parameter to get the objects if the value of the property does not match with the given regular expression. This operation is case-sensitive.
-Contains
The Where-Object cmdlet used this parameter to get the objects from the collection if the property value of an object matches with the given value.
-CContains
The Where-Object cmdlet used this parameter to get the objects if the item in the property value of an object matches with the given value. This operation is case-sensitive.
-NotContains
The Where-Object cmdlet used this parameter to get the objects if none of the items in the property value of an object matches with the given value.
-CNotContains
The Where-Object cmdlet used this parameter to get the objects if an item in the property value of an object does not match with the given value. This operation is case-sensitive.
-In
The Where-Object cmdlet used this parameter to get the objects if the value of the property matches any of the given values.
-CIn
The Where-Object cmdlet used this parameter to get the objects if the value of the property matches any of the given values. This operation is case-sensitive.
-NotIn
The Where-Object cmdlet used this parameter to get the objects if the value of the property does not match any of the given values.
-CNotIn
The Where-Object cmdlet used this parameter to get the objects if the value of the property does not match any of the given values. This operation is case-sensitive.
-Is
The Where-Object cmdlet used this parameter to get the objects if the value of the property is an instance of a given .NET Framework type. You must enclose the name of the type in square brackets.
-IsNot
The Where-Object cmdlet used this parameter to get the objects if the value of the property is not an instance of a given .NET Framework type.
Examples
Example 1: Get the stopped Services
Both the commands in this example get a list of all the services which are currently stopped. The first command uses the format of a script block, and the second command uses the format of a comparison statement.
Example 2: Get the processes on the basis of the process name
The command in this example gets the name of services that start with the A letter. The -Match parameter allows you to use a regular expression.