Updated March 4, 2023
Introduction to PowerShell where
PowerShell Where-Object cmdlet (Alias: where) selects or filters out the particular properties of the object based on its value that are passed to it using Pipeline, like to filter out the particular process with their names or usage or to filter out the service based on their startup type, status, etc. This Where-Object takes the input from the PowerShell Pipeline and process on it and it uses the comparison operator along with the value of the property and it works either with the ScriptBlock or comparison statement based.
Syntax
Syntax of Where-Object command.
Syntax 1:
Where-Object
[-InputObject <PSObject>]
[-Property] <String>
[[-Value] <Object>]
[-Comparision Operator]
[<CommonParameters>]
Here the comparison operators are,
EQ | GT |
MATCH | CGT |
CEQ | LT |
CNE | CLT |
GE | CGE |
LE | CLE |
Like | Clike |
NotLike | CNotLike |
Cmatch | NotMatch |
CNotMatch | Contains |
CContains | NotContains |
CNotContains | In |
CIn | NotIn |
CNotIn | Is |
IsNot | Not |
Syntax 2:
Where-Object
[-InputObject <PSObject>]
[-FilterScript] <ScriptBlock>
[<CommonParameters>]
Common parameters for both the syntax.
Debug (db) | OutVariable (ov) |
ErrorAction (ea) | OutBuffer (ob) |
ErrorVariable (ev) | PipelineVariable (pv) |
InformationAction (infa) | Verbose (vb) |
InformationVariable (iv) | WarningAction (wa) |
WarningVariable (wv) |
How does the Where-Object command work in PowerShell?
Where-Object cmdlet in PowerShell takes the Input from the Pipeline and it works on the input object properties. Input objects can be objects consist of the methods and properties or it can be an array. Once you use the where-object cmdlet, it uses the property of the object to filter out along with the comparison operator.
There are two ways to construct a Where-Object command.
1. ScriptBlock
In this method, we use the Property name, comparison operators, or Logical operators, and Property value to filter out the data from the object. Where-Object returns all the objects for which a particular script block statement is true.
For example,
Get-Process | Where-Object {$_.Name -eq "Chrome"}
2. Comparison Statement
This feature was introduced in PowerShell 3.0 and before that, there was only a scriptblock method. This method uses the natural language for filtering out the object by providing the property value.
For example,
Get-Process | Where-Object -Property "Name" -EQ -Value "Chrome"
When only the name of the property is provided but not the value, it treats Where-Object as a Boolean expression. For example,
('Hello','','PowerShell') | Where-Object Length
Few more examples will see in the examples section.
We can also use the ‘?’ symbol instead of the Where-Object command. For example,
Get-Service | ?{$_.StartType -eq 'Disabled'}
Values we provide in the Where-Object command are not case sensitive but can be made case sensitive using the few operators which starts which ‘C’ (ceq, clike, Ccontains, etc).
Examples
Here are the following examples mention below
Example #1 – Using -EQ operator.
In this example, Where-Object uses -EQ operator to get the start type of the service in the Disabled State.
Get-Service | where{$_.StartType -eq 'Disabled'}
Or
Get-Service | Where-Object -Property Starttype -EQ -Value Disabled
Output:
Example #2 – Using Multiple comparison operators and a logical operator.
We can combine multiple comparison operators and a logical operator to filter the result out with the Where-Object command. For example,
Get-Service | Where{($_.StartType -eq 'Manual') -and ($_.Status -eq 'Running')} | Select Name, Starttype, Status
Output:
Example #3 – Using Wildcard character.
We can use the wildcard character with the Like operator to get similar results. For example,
To get the service names starting with ‘P’.
Get-Service | where{$_.Name -like 'P*'}
Output:
Service Name ending with ‘A’.
Get-Service | where{$_.Name -like '*A'}
Output:
Service name containing ‘Logs’.
Get-Service | where{$_.Name -like '*Log*'}
Output:
Example #4 – Case Sensitivity.
PowerShell is non-case-sensitive and their commands but it can be made case sensitive using some comparison operators and they start with the ‘C’.
Get-Service | where{$_.Name -ceq 'Netlogon'}
The above command gives the output but because Netlogon exactly matches with the name of the service but the below command doesn’t produce output because of the name case sensitivity with -CEQ operator.
Get-Service | where{$_.Name -ceq 'netlogon'}
Output:
Example #5 – Filtering the array with Length.
Consider we have the following array and to filter the non-null output, we can use the below command.
"John","","Alex","" | Where-Object Length -gt 0
Output:
Similarly, you can also filter the files.
Example #6 – Using (Greater, Less) than operators.
We can also use a few operators for the length comparison. For example, the below command will get all the processes whose handle is greater than equal to 1000, and in the next example, it’s less than 5000.
Get-Process | ?{$_.Handles -ge 1000}
Get-Process | ?{$_.Handles -lt 1000}
Similarly, you can use operators -Gt (Greater than), -le (less than equal to) to compare integer properties.
Example #7 – Using True or False property.
In the above examples, we have used the PowerShell Where-Object command with properties but properties with the True or False output can be directly used in the where command. We will see both the Command based and scripblock based examples.
Get-Process | where Responding
The above example will get all the processes that are responding and the same command can be written with scripblock below.
Get-Process | Where-Object{$_.Responding -eq $true}
For not responding process,
Get-Process | where -Property Responding -NE $true
With the scriptblock,
Get-Process | where{!($_.Responding -eq $true)}
Output:
Example #8 – Where-Object with -Contains Operator.
To get the objects or an array value we can use the operator Contains. For example, the below command will search from the Process output using the -Contains operator.
Get-Process | where{$_.Name -contains "Chrome"}
To filter with the case sensitive content, use the -CContains operator.
Example #8 – Where-Object use without object property.
An object like an array doesn’t have the Property name, in that case, we can use, $_ to filter the result with Where-Object without specifying the name of the property.
"Hello","PowerShell","Azure","Office365" | where{$_ -eq 'PowerShell'}
Output:
Conclusion
Where-Object in a PowerShell is considered a backbone for the PowerShell results because it can filter out thousands of results from files or the objects within a few seconds or minutes and which helps Powershell to process faster on the result. It is very much helpful for data processing and filtering the content.
Recommended Articles
This is a guide to PowerShell where. Here we discuss How does the Where-Object command work in PowerShell along with the examples and outputs. You may also have a look at the following articles to learn more –