EDUCBA

EDUCBA

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

PowerShell Grep

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Grep

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.

Syntax

The below is the syntax of Select-String:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

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

View Course

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

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

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
    • 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
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • 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 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
  • 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
  • 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
  • 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
  • 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

© 2022 - 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

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 Login

Forgot Password?

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.

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.

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

Special Offer - Shell Scripting Course Learn More