You are currently viewing PowerShell Where-Object Cmdlet

PowerShell Where-Object Cmdlet

PowerShell Where-Object Cmdlet

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Sort-Object cmdlet in PowerShell.

https://cloudaffaire.com/powershell-sort-object-cmdlet/

In this blog post, we will discuss the Where-Object cmdlet in PowerShell. You can use the Where-Object cmdlet to filter objects based on certain criteria in PowerShell. You need to define the property name and the condition for filtration and where the object will return the objects which fulfill the condition. For example, suppose you want to get services that are stopped. You can use Get-Service | Where-Object Status -eq Stopped command to get all stopped services. The condition in Where-Object cmdlet can be provided in two ways. You can use a script block to specify the property name, a comparison operator, and a property value. Where-Object returns all objects for which the script block statement is true. You can also write a comparison statement, which is much more like natural language.

Where-Object Cmdlet Syntax:

Where-Object Cmdlet Argument List:

  • FilterScript: Specifies the script block that is used to filter the objects. Enclose the script block in braces ({}).
  • InputObject: Specifies the objects to be filtered. You can also pipe the objects to Where-Object.
  • Property: Specifies the name of an object property. The parameter name, Property, is optional.
  • Value: Specifies a property value. The parameter name, Value, is optional.
  • Contains: Property value of the object is an exact match for the specified value.
  • CContains: Property value of the object is an exact match for the specified value. This operation is case-sensitive.
  • NotContains: If none of the items in the property value is an exact match for the specified value.
  • CNotContains: If the property value of the object is not an exact match for the specified value. This operation is case-sensitive.
  • EQ: Property value is the same as the specified value.
  • CEQ: Property value is the same as the specified value. This operation is case-sensitive.
  • NE: Property value is different than the specified value.
  • CNE: Property value is different than the specified value. This operation is case-sensitive.
  • GE: Property value is greater than or equal to the specified value.
  • CGE: Property value is greater than or equal to the specified value. This operation is case-sensitive.
  • GT: Property value is greater than the specified value.
  • CGT: Property value is greater than the specified value. This operation is case-sensitive.
  • In: Property value matches any of the specified values.
  • CIn: Property value includes the specified value. This operation is case-sensitive.
  • NotIn: Property value is not an exact match for any of the specified values.
  • CNotIn: If the property value is not an exact match for the specified value. This operation is case-sensitive.
  • LE: Property value is less than or equal to the specified value.
  • CLE: Property value is less-than or equal to the specified value. This operation is case-sensitive.
  • Like: Property value matches a value that includes wildcard characters.
  • CLike: Property value matches a value that includes wildcard characters. This operation is case-sensitive.
  • NotLike: If the property value does not match a value that includes wildcard characters.
  • CNotLike: If the property value does not match a value that includes wildcard characters. This operation is case-sensitive.
  • LT: Property value is less than the specified value.
  • CLT: Property value is less-than the specified value. This operation is case-sensitive.
  • Match: Property value matches the specified regular expression.
  • CMatch: Property value matches the specified regular expression. This operation is case-sensitive.
  • NotMatch: If the property value does not match the specified regular expression.
  • CNotMatch: If the property value does not match the specified regular expression. This operation is case-sensitive.
  • Is: Property value is an instance of the specified .NET type. Enclose the type name in square brackets.
  • IsNot: If the property value is not an instance of the specified .NET type.

PowerShell Where-Object Cmdlet:

Filter Output Based On Condition In PowerShell:

Filter Output Based On Multiple Conditions In PowerShell:

Hope you have enjoyed this article. In the next blog post, we will discuss ForEach-Object cmdlet in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply