EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Grep
 

PowerShell Grep

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

PowerShell Grep

 

 

Introduction to PowerShell Grep

Grep command is not a PowerShell cmdlet. It is a command used in Linux/Unix which allows the user to filter based on various patterns. The same is achieved in PowerShell via the Select-String cmdlet. It can be considered as the GREP equivalent in windows. Select-String is used for comparing text and patterns in both files as well as input string. It uses regular expression for matching purposes. It searches for patterns on a line by line basis. Whenever a match is found on a line, corresponding file name, line number and the match are printed. While searching, if the user wants a encoding to be used for comparison even that can be specified.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

The below is the syntax of Select-String:

NAME
Select-String
SYNTAX
Select-String [-Pattern] <string[]> [-Path] <string[]> [-SimpleMatch] [-CaseSensitive] [-Quiet] [-List] [-Include <string[]>] [-Exclude <string[]>] [-NotMatch] [-AllMatches] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}] [-Context <int[]>]  [<CommonParameters>] Select-String [-Pattern] <string[]> -InputObject<psobject> [-SimpleMatch] [-CaseSensitive] [-Quiet] [-List] [-Include <string[]>] [-Exclude <string[]>] [-NotMatch] [-AllMatches] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}] [-Context <int[]>]  [<CommonParameters>] Select-String [-Pattern] <string[]> -LiteralPath<string[]> [-SimpleMatch] [-CaseSensitive] [-Quiet] [-List] [-Include <string[]>] [-Exclude <string[]>] [-NotMatch] [-AllMatches] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}] [-Context <int[]>]  [<CommonParameters>] ALIASES
sls

Parameters

Below are the different parameters of PowerShell Grep

1. -AllMatches: This denotes that all matching patterns in each line must be returned. Without this, Select-String matches only the first matching pattern in a line. Its type is switch parameter. Its default value is false. It doesn’t accept pipeline input and wild card characters are not accepted.

2. -CaseSensitive: It denotes that a case sensitive match must be performed. By default, the matches are case-insensitive. Its type is switch parameter. Its default value is false. It doesn’t accept pipeline input and wild card characters are not accepted.

3. -Context: This captures the number of lines before a match is found and the number of lines that there after the match. If only a single number is mentioned, that number determines the lines before and after the match. If two numbers are mentioned, first number denotes the number of lines before the match and the number of lines after the match is determined by the second number. Its type is int32[]. Its default value is none. It doesn’t accept pipeline input and wild card characters are not accepted.

4. -Culture: It denotes the culture name to be used for matching purpose. To get a list of available cultures the Get-Culture -ListAvailable cmdlet can be used. It was first introduced with PowerShell 7. Its type is string. Default value is the culture value of the current PS session. It doesn’t accept pipeline input and wild card characters are not accepted.

5. -Encoding: It denotes the target files encoding type. The default encoding type is UTF8NoBOM. The other accepted values are ASCII, BigEndianUnicode, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32. It doesn’t accept pipeline input and wild card characters are also not accepted.

6. -Exclude: This denotes the list of items that needs to be excluded from matching. It can be a path or a pattern. Its type is String []. Its default value is none. It can accept wild card characters whereas pipeline input is not allowed.

7. -Include: This denotes the list of items that needs to be included for matching. It can be a path or a pattern. Its type is String []. Its default value is none. It can accept wild card characters whereas pipeline input is not allowed.

8. -InputObject: It denotes the text to be searched. It can either be a variable that holds a text or an expression. Its type is PSObject. Its default value is none. It accepts pipeline input whereas wild card characters are not accepted.

9. -List: When this is used, only the first instance of matching is considered and returned for each file. Its type is switch parameter. Its default value is false. It doesn’t accept pipeline input and wild card characters are also not allowed.

10. -LiteralPath: It denotes the path of the files to be searched. The value is same as how it is typed. No wild card characters are allowed, if the path has special characters it must be enclosed in single quotes. Its type is string []. Its aliases are PSPath and LP. Its default value is none. It accepts pipeline input, but wild card characters are not accepted.

11. -NoEmphasis: Select-String by default that matches the search pattern, to disable the highlighting this parameter is used. This was first introduced in PowerShell 7. Its type is switch parameter. Its default value is false. It doesn’t accept pipeline input and wild card characters are not accepted.

12. -Pattern: This denotes that pattern that needs to be searched for. Its treated as a regular expression. Its type is String []. Its default value is none. It doesn’t accept pipeline input and wild card characters are also not allowed.

13. -Path: It denotes the path of the files to be searched. Its default value is current directory. It accepts pipeline input; also, wild card characters are accepted.

14. -NotMatch: It is used to find the non-matching occurrence. Its type is switch parameter. Its default value is false. It doesn’t accept pipeline input and wild card characters are also not allowed.

Example of PowerShell Grep

Given below is the example:

Code:

Write-Host "Welcome to Select String example in Powershell"
$input=@("one","two","three","four","ttt","thousand","theew")
Write-Host "Demo of select in an array"
$input| Select-String -Pattern 'th'
Write-Host "Searching a single file"
Select-String -Pattern Scri -Path "C:\Vignesh\Test\AD Group export to csv.txt"
Write-Host "Matching multiple files"
Get-ChildItem C:\Vignesh\Test\*.txt -Recurse |Select-String -Pattern Scri
Write-Host "Finding the number of non matches"
$result=Select-String -Pattern Scri -NotMatch -Path "C:\Vignesh\Test\AD Group export to csv.txt"
Write-Host "Total number of non matches" $result.Count

Output:

powershell grep

Conclusion

Thus, the article explained in detail how grep can be achieved in PowerShell using Select-String cmdlet. It explained about the various types of parameters that are available for the cmdlet along with appropriate example.

Recommended Articles

This is a guide to PowerShell Grep. Here we discuss the introduction and example of PowerShell Grep. You may also have a look at the following articles to learn more –

  1. PowerShell Get-Process
  2. PowerShell Concatenate String
  3. PowerShell Get-Item
  4. PowerShell Get-Content
  5. Types of PowerShell Wildcards
  6. PowerShell If-Not | Examples
  7. Learn the PowerShell Rename Folder

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW