EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell where
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • 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 Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • 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 Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • 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 do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell where

PowerShell where

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>]

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Here the comparison operators are,

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,171 ratings)
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:

PowerShell where output 1

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:

PowerShell where output 2

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:

PowerShell where output 3

Service Name ending with ‘A’.

Get-Service | where{$_.Name -like '*A'}

Output:

PowerShell where output 3.2

Service name containing ‘Logs’.

Get-Service | where{$_.Name -like '*Log*'}

Output:

PowerShell where output 3.3

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

PowerShell where output 4

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:

output 4.2

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:

output 5

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:

output 7

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:

output 8

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 –

  1. PowerShell Sleep
  2. PowerShell Filter
  3. PowerShell Registry
  4. PowerShell Continue
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more