EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Where Object

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Where Object

PowerShell Where Object

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

PowerShell Where Object syntax

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.

Popular Course in this category
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,494 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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"

PowerShell Where Object Example 1PowerShell Where Object Example 1

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"}

PowerShell Where Object Example 2

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 3

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.*"

Example 4

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 –

  1. How to Use PowerShell?
  2. PowerShell Run Command
  3. Get-Command in PowerShell
  4. PowerShell Format Table | How to Format Table?

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • cmdlet
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • PowerShell Out-File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
  • Basics
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Modules
    • Variable in PowerShell
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell vs PowerShell ISE
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell Wait
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Rename Folder
    • PowerShell String Replace
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell XML
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV 
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Shell Scripting Course Learn More