The grep command is the most powerful UNIX utility for searching any given input files and matching using regular expressions and patterns. If you are looking for a command that works like the grep, you can make use of the Select-String cmdlet.
The Select-String cmdlet can be found in the Microsoft.PowerShell.Utility module of PowerShell. It can be used to find text in strings and files.
Syntax:
Select-String
[-Culture <String>]
[-Pattern] <String[]>
[-Path] <String[]>
[-SimpleMatch]
[-CaseSensitive]
[-Quiet]
[-List]
[-NoEmphasis]
[-Include <String[]>]
[-Exclude <String[]>]
[-NotMatch]
[-AllMatches]
[-Encoding <Encoding>]
[-Context <Int32[]>]
[<CommonParameters>]
Let's take a look at a few examples.
Example 1: Simple Text Matching
PS> 'Welcome to Code2care' | Select-String -Pattern 'Code2care' -SimpleMatch
Welcome to Code2care
Example 2: Simple Text Case Sensitive Matching
PS> 'Welcome to Code2care' | Select-String -Pattern 'Code2care' -SimpleMatch -CaseSensitive
Welcome to Code2care
Example 3: Find Matching Text Within a file
PS> Select-String ./file1.txt -Pattern "PowerShell"
file1.txt:2:PowerShell is fun!
Above examples are based on PowerShell Core version 7.3 on macOS.
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Powershell,
- PowerShell: How to Check if a Module is Already Installed
- PowerShell: How to Write Output to a File
- PowerShell: Steps to Connect to connect to Exchange Online
- PowerShell Fix: Get-Help cannot find the Help files for this cmdlet on this computer
- PowerShell Traditional For Loop Example
- How to Run PowerShell Script as Administrator (Elevate)
- Update Powershell Using Command Line
- How to delete a file using PowerShell [Windows/macOS]
- PowerShell: Grep Command Alternative - Select-String
- PowerShell Concatenate String Examples
- How to add Sleep to PowerShell Script
- Connect Azure AD (Active Directory) for PowerShell
- How to Open PowerShell on Mac?
- List of PowerShell Function Commands for Mac
- How to Connect to Azure AD Account using PowerShell on Mac
- Download Google Chrome setup exe file using PowerShell
- Set Environment Variable in PowerShell for Mac
- How to switch to Powershell on Mac Terminal
- How to add sleep in Powershell Script
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program
- How to Identify installed PowerShell version
- How to check PowerShell version
- How to install PowerShell on Mac using Brew
- PowerShell Switch Statement with Examples
- PowerShell ISE Alternative for Mac
More Posts:
- How to check file permissions for your file using Linux/Unix/macOS Terminal Command - Linux
- Download a SSL Certificate from a URL in Terminal - Bash
- Eclipse like Auto Import Shortcut in Intelij IDE Android Studio - Android-Studio
- How to delete a file using Python code example - Python
- SharePoint 2010 August 2015 Update KB3055049 - Duplicate Document ID issue bug fixed - SharePoint
- Google Chrome Update failed (error: 12) - Chrome
- SharePoint installation - Appfabric installation failed because installer MSI returned with error code:1603 - SharePoint
- Where is AWS Config and Credential files located on Mac - AWS