Select-string is utilized to compare text and patterns by using regular expressions for this process. It implements the search process by searching line by line. When it finds the match, the file name, line number, and match are printed.
In this post, the working and usage of PowerShell’s grep equivalent command are described in detail.
How to use PowerShell grep Equivalent
As we have come to know that Grep is a Linux-based command and is used in Linux to find and select the text strings in a log file. Like grep, Select-string is used to find the text patterns and files using regular expressions in PowerShell. The upcoming examples better demonstrate the grep equivalent in PowerShell.
Example 1: How to find the specified services in PowerShell?
The Get-Service cmdlet prints all the services (either Running or Stopped) on the system. The following code uses the “Get-Service” and “Select-String” cmdlets to get the list of stopped services only.
Out-String is used to convert each parameter object into a string rather than concatenating all the objects into a single file.
> Get-Service | Out-String -Stream | Select-String «STOPPED»
The output shows that only the stopped services are displayed on the console. The above command will get all the processes and it will only those processes which are currently stopped.
Example 2: How to find the selected word in a directory using PowerShell?
The Select-String cmdlet can be used to find a specific word inside a directory. To do this, we will first give the path of the folder and then we will create the Select-String condition after the pipeline. The pipeline operator (|) is used to provide the input (which is the output of the previous command) to the next command.
The command below will search the pattern “PowerShell” inside the files of the “C:Test” directory:
> Get-ChildItem -Path C:Test | Select-String -Pattern ‘PowerShell’
The output shows that there are two files inside the “C:Test” directory that contains the “PowerShell” keyword.
Example 3: How to find the strings in the sub directory using PowerShell?
The “-Recurse” flag is used to perform a specific operation on sub-directories. The following command checks the “PowerShell” keyword inside the “C:Test” directory. We have used the “-Filter” parameter to perform the operation only on files with the “.txt” extension.
> Get-ChildItem «C:Test» -Filter *.txt -Recurse | Select-String «PowerShell»
The output describes that three “.txt” files contain the “Powershell” keyword.
Example 4: How to find a string with additional information in PowerShell?
The Select-String cmdlet can be used to get the additional information of the searched keyword. For instance, the following command makes use of the “Select-String” cmdlet to check the “PowerShell” keyword in the text files in “C:Test”. The “Select” keyword is used to select the information of the files that contain the “PowerShell” keyword.
> Get-ChildItem «C:Test» -Filter *.txt -Recurse | Select-String «PowerShell»| Select Filename, LineNumber, Line, Path | Format-Table
It can be observed from the output that “Filename”, “LineNumber”, “Line”, and “Path” of the files are printed.
Here you go! You have learned the basics and advanced level working/usage of the PowerShell grep equivalent.
The Select-String cmdlet is the grep equivalent in PowerShell. The grep is the Linux and UNIX-like command which is used to find the regular expressions. This post defines and explains the PowerShell grep equivalent command. The Select-String cmdlet can be used to get the files that contain a specific pattern. It can also be used to get additional information on the files containing a specific string or text.