Updated March 24, 2023
Introduction to PowerShell Where Object
Where-Object allows us to select or get a particular object, file, process, services, etc. on the basis of certain properties of that object, file, process, services. These properties may be size, name, date, etc. Let me give you an example, suppose you have a folder containing 1000 files and you want to perform some task on the file which created most recently or the file which was created first. So We can select the file date wise. Another example, suppose we want to select the largest file among all file so we can put where the condition for the size of the file. We can also write conditions for getting information on any services, packages, processes, etc. In this topic, we are going to learn about PowerShell Where-Object.
How to Create Where Object command in Powershell?
There are mainly two ways to create Where-Object command they are given below.
1. Script block
We can go with Script block to define property name, comparison operator and property value. The where-object command will check and return objects for which block is true.
Get-Process | Where-Object {comparison condition}
2. Comparison statement
In this way, we can directly use comparison like we use any normal programming.
Get-Process | Where-Object condition
Syntax:
A very simple way to get all syntax and parameters is to run the below command. All the given below syntax will be used in different kinds of situations. We will discuss some of them.
Get-Help Where-Object -full
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] [-InputObject <psobject>] [-EQ] [<CommonParameters>]
Where-Object [-FilterScript] <Here we write script containing conditions> [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -GE [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CEQ [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -NE [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CNE [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -GT [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CGT [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -LT [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CLT [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CGE [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -LE [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CLE [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -Like [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CLike [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -NotLike [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CNotLike [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -Match [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CMatch [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -NotMatch [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CNotMatch [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -Contains [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CContains [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -NotContains [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CNotContains [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -In [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CIn [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -NotIn [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -CNotIn [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -Is [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> [[-Value] <Object>] -IsNot [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <Property like date,size etc> -Not [-InputObject <psobject>] [<CommonParameters>]
Parameters of PowerShell Where Object
Here are the Parameters listed below.
1. CContains: It checks for if exact match, which means it will return an object only if the exact object match will work.
The general syntax for it is given below.
Get-Process | where servicename/processname/etc -CContains "exact matching name
2. CEQ: If the same property value is the same then only it gets the object
3. CGE: If the property value is greater than or equal to the passed value then only it gets the object.
4. CGT: If the property value is greater than the passed value then only it gets the object. Remember this condition is completely case-sensitive.
5. CIn: This will check for include, that is if property value include then it gets the object
6. CLE: This will check for less than or equal to the passed value.
7. CLT: This will check for less-than on passed value.
8. CLike: This will check for match property value, it also includes wildcards. Also, this operation is case sensitive.
9. CMatch: This property is based on regular expressions, So in where condition we can also check for with some regular expressions.
10. CNE: This command will be used with where condition if we are trying to find different then passed value.
11. CNotContains: It will get the object in case if the property value is not exactly matching to the passed value. This command condition is case sensitive.
Examples
Here are the Examples to implement PowerShell Where Object mention below
Example #1
In the below example we are checking services with the name “nginx” exact match. It returns all the running services with the name nginx. Please refer to the example along with the screen below.
Get-Process | where ProcessName -CContains "nginx"
Example #2
In this command we are using -eq to get all match package with a specific version. First, we display all versions by running command Get-PackageProvider and than we searched for a specific version bypassing version exact in a script block.
Get-PackageProvider
Get-PackageProvider | Where-Object {$_.Version -eq "3.0.0.1"}
Get-PackageProvider | Where-Object {$_.Version -eq "2.1.3.0"}
Example #3
Here In this example, we wanted to know all the running processes with a name starting with “n”. In the output, we can see it is displaying “node nginx networkmanager netns, etc”. You can try it on your own system you will get different output according to running processes on your system with a name starting with “n”.This Where-object example is blocking script type.
Get-Process | Where-Object {$_.ProcessName -Match "^n.*"}
Example #4
As we discussed at the time of introduction that there are two types of Where-Object, one is block script and another one is caparison. In example 3 we discussed block script, In this example, we are understanding caparison statement block.
Get-Process | Where-Object ProcessName -Match "^n.*"
Conclusion
Where-Object used for fetching some object, files, process, services, etc. on the basis of certain conditions, these conditions may be property, name, size or date of the object, files, process, services.
Recommended Articles
This is a guide to PowerShell Where Object. Here we discuss the Parameters and Examples to implement PowerShell Where Object. You may also have a look at the following articles to learn more –