Home » PowerShell » Search String in File or Grep in PowerShell

Search String in File or Grep in PowerShell

PowerShell Grep equivalent cmdlet is Select-String which uses regular expressions to search string in files. Select-String cmdlet in Powershell works similar to grep in UNIX and findstr in PowerShell.

Select-String searches for text or text patterns in the input string or file. It displays the file name, line number, and text in the line containing the match.

PowerShell grep equivalent Select-String
PowerShell grep equivalent Select-String

In this article, we will discuss PowerShell grep equivalent Select-String to search string in the files recursively using the Get-ChildItem, search string in the text that matches the patterns.

PowerShell Search String in File

Get-ChildItem in PowerShell gets one or more child items based on search criteria.

Using the Select-String cmdlet in PowerShell with Get-ChildItem to search for the string in the file recursively.

Get-ChildItem -Path D:\PowerShell\ -Recurse | Select-String -Pattern 'PSIsContainer' 

In the above PowerShell script, Get-ChildItem uses the parameter -Recurse to get one or more child items for the path specified and pipe output to the Select-String command.

PowerShell Tip: How to search for files in PowerShell!

Select-String uses a Pattern parameter to specify a string to find string in the file.

On successful match of string in a file, Select-String returns the filename, line number, and a line containing the search text in it.

The output of the grep equivalent Select-String command to find string in file is:

PowerShell search string in the file
PowerShell search string in the file

PowerShell find string in file

Use Select-String in PowerShell to find a string in the file. It uses regular expression matching to search for patterns in the file.

 Select-String -Path D:\PowerShell\*.* -Pattern 'Get-'  

In the above PowerShell script, Select-String uses Pattern ‘Get-‘ to search string in the file specified by the Path parameter.

The output of the script to grep a text file is:

PS C:\> Select-String -Path D:\PowerShell\*.* -Pattern 'Get-'   
                                    
D:\PowerShell\GetFileProperties-Assignment.txt:1:Get-ChildItem -File | Select name,CreationTime
D:\PowerShell\print-spooler-service-Test.ps1:1:$PrintSpooler = Get-Service -Name Spooler
D:\PowerShell\print-spooler-service.ps1:1:$PrintSpooler = Get-Service -Name Spooler

Select-String displays the output on the console. It finds the string in the file and prints its filename, line number, and line containing the text.

PowerShell Tip: How to find the file last modified date using PowerShell!

Search for String in File Using Get-Content

The Get-Content gets the content of the file specified by the path parameter. It reads the content of the file and returns the string object.

Use the Get-Content to search for a string in the file using the following command.

Get-Content -Path C:\Windows\my.ini
Get-Content -Path C:\Windows\my.ini | Select-String -Pattern "basedir*"     

In the above PowerShell script,

The Get-Content reads the content of the file my.ini specified by the Path parameter.

It pipes the content of the string object to the Select-String command. It uses the Pattern parameter to find the string in the string object.

The output of the above script to search string in string object is:

Get-Content search string in object
Get-Content search string in object

Search String in Hash variable

Using the Select-String in PowerShell, you can search string in the hash variable.

let’s consider an example where hash contains the below key-value data pair and is stored in the $mysqlInfo variable.

$mysqlInfo = @{
basedir='C:/Program Files/mysql'

datadir='C:/Program Files/mysql/data'

port='3306'

}

Use the Select-String to specify the pattern to find the string in the variable.

$mysqlInfo |Out-String -Stream | Select-String -Pattern 'datadir'

In the above PowerShell script, the $mysqlInfo variable input pipes to the Out-String -Stream. It formatted the string objects into multiple single-line string objects.

Select-String uses the Pattern parameter to specify the input string to search in variable and returns the output as follows:

$mysqlInfo |Out-String -Stream | Select-String -Pattern 'datadir'

datadir          C:/Program Files/mysql/data   

PowerShell Tip: PowerShell Select-String Ignore Case search and CaseSensitive Search!

Conclusion

I hope the above article using the Select-String to search string in file and variable is helpful to you.

Use Select-String to find the string in file or variable similar to the grep command in UNIX and findstr in windows.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.