EDUCBA

EDUCBA

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

PowerShell Filter

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Filter

PowerShell Filter

Introduction to Filtering in PowerShell

Filtering refers to the process of restricting the output of a cmdlet or a statement based on certain conditions. This helps in optimizing the results and the user will be able to see what he wants from the cornucopia of results. This is achieved in PowerShell with the help of the Where-object cmdlet. Where the object will select the objects in the collection that matches the condition. The where objects can be used in two ways, either using script block or using conditional statements. This article will cover in detail the various filters that are available in PowerShell.

Syntax

The following is the syntax of the script block where the condition

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Where-Object[-InputObject <PSObject>][-FilterScript] <ScriptBlock>[<CommonParameters>]

OR

Get-Process | Where-Object {$_.Id -eq 5940}

The following is the syntax of the condition operator:

Where-Object[-InputObject <PSObject>][-Property] <String>[[-Value] <Object>][-Comparison Operator][<CommonParameters>]

OR

Get-Process | Where-Object ID -eq 5940

To get the list of properties that are available for a cmdlet, the Format-List cmdlet can be piped to the respective cmdlet to list the available properties.

To select only a handful of objects from the collection the Select-Object cmdlet can be used.

OR

Get-Process | Select-Object -First 5

The above cmdlet will only display the first five processes.

Different Types of Filter

Different types and their use:

Contains

Demonstrates that this gets objects if anything within the property esteem of the question is an exact coordinate for the desired value. If the property esteem contains a single question, PowerShell changes over it to a collection of one object. This parameter was presented in Windows PowerShell 3.0. The data type is the switch parameter. None is the default esteem. It is referred by the alias It Contains. Pipeline input is not accepted, and wild card characters are also not permitted.

Popular Course in this category
PowerShell Training (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 4+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,023 ratings)
Course Price

View Course

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

Eq

This will return objects from the collection whose property value is the same as the given value. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias IEQ. Pipeline input is not accepted, and wild card characters are also not permitted.

FilterScript

This denotes the script block that should be used for filtering. The script block is enclosed within ({}). Pipeline input is not accepted, and wild card characters are also not permitted.

GE

This will return objects from the collection whose property value is greater than or equal to the given value. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias IGE. Pipeline input is not accepted, and wild card characters are also not permitted.

GT

This will return objects from the collection whose property value is greater than the given value. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias IGT. Pipeline input is not accepted, and wild card characters are also not permitted.

In

This will return the objects whose values are one of the specified values. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias IIn.Pipeline input is not accepted, and wild card characters are also not permitted.

Is

This will return objects whose datatype is the same as the value mentioned. The data type of the parameter is the switch parameter. None is the default esteem. Pipeline input is not accepted, and wild card characters are also not permitted.

IsNot

This is the opposite of Is. This will return objects that don’t match the specified type. The data type of the parameter is the switch parameter. None is the default esteem.  Pipeline input is not accepted, and wild card characters are also not permitted.

LE

This will return objects from the collection whose property value is less than or equal to the given value. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias ILE. Pipeline input is not accepted, and wild card characters are also not permitted.

LT

This will return objects from the collection whose property value is less than the given value. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias ILT. Pipeline input is not accepted, and wild card characters are also not permitted.

Like

This returns objects that satisfy the wildcard condition. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias ILike. Pipeline input is not accepted, and wild card characters are also not permitted.

Match

This returns objects that satisfy the regular expression. The data type of the parameter is the switch parameter. None is the default esteem.  It is referred by the alias IMatch. Pipeline input is not accepted, and wild card characters are also not permitted.

Example to Implement PowerShell Filter

Below is the example mentioned:

Example #1

Code:

Write-Host "Welcome to filtering example"
Write-Host "Demo of contains example"
$names = 'vignesh','nandhini','vyapini','vijay','sethu'
$names -contains 'vignesh'
Write-Host "Match is found"
$age=19
Write-Host "Demo of eq operator"
$ee= Read-Host "Enter age"
if($ee -eq $age)
{
Write-Host "age matches"
}
else
{
Write-Host "doesnt match"
}
Write-Host "greater than or greater than equal to example"
$ee= Read-Host "enter a number"
if(($ee -ge 100) -and ($ee -le 200))
{
Write-Host "entered no is greater than or equal to 100 and less than or equal to 200"
}
elseif(($ee -gt 200) -and ($ee -lt 300))
{
Write-Host "entered no is greater than 200 and less than 300"
}
else
{
Write-Host "entered no is greater than 300"
}
Write-Host "example of in"
$val= "aa","bb","cc"
Write-Host "enter a value"
$tt= Read-Host "enter a value"
if($tt -in $val)
{
Write-Host "value in matched"
}
else
{
Write-Host "no match"
}

Output:

powershell filter1

Conclusion

Thus, the article covered in detail about the filtering operations in PowerShell. It covered with appropriate examples of the various operators that are available for filtering. To learn more in detail it is advisable to write sample scripts and practice them.

Recommended Articles

This is a guide to PowerShell Filter. Here we discuss an introduction to PowerShell Filter, syntax, different types, and example with sample code. You can also go through our other related articles to learn more –

  1. PowerShell Versions
  2. PowerShell Set-Content
  3. PowerShell Get-ChildItem
  4. How to Use PowerShell?

PowerShell Training (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

4+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • 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 Get-ADUser
    • PowerShell Start-Process
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV 
    • PowerShell Export CSV
  • Basics
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • 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
  • 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 Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
  • 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
  • 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
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 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

Special Offer - PowerShell Training (2 Courses, 1 Project) Learn More