
Use the Where-Object cmdlet if you need to grep objects (Filtering PowerShell objects using Where-Object).įor example, you want to find all processes that use more than 300 MB of RAM. The Select-String cmdlet allows you to process string data.

Get-ChildItem C:\logs\ -Filter *.log -Recurse | Select-String “Failed”| Copy-Item -Destination C:\Errors You can copy all files which match to the specified directory: Get-ChildItem “C:\PS\” -Filter *.log -Recurse | Select-String “Failed”| Select Filename, LineNumber, Line, Path | Format-Table To display the names of all found files and matching line numbers: Get-ChildItem “C:\PS\” -Filter *.log -Recurse | Select-String “Failed”|

If you need to check all sub-folders, use the -Recurse parameter: Get-Childltem -Path C:\PS | Select-String -Pattern ‘Error’ You can use the regular expressions (RegEx) in the Select-String cmdlet.įor example, the following command will find all files or lines containing the word “Error” in the specified folder: Get-Service | Out-String -Stream | Select-String “Running” If the previous pipe command returns objects other than the text, use the Out-String –Stream command before passing them to Select-String: The following encodings are supported: ASCII, BigEndianUnicode, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32.įor example, use the following command to find a list of open ports on your computer (with the Listening status): Select-String allows specifying the encoding of the input file (parameter -Encoding). In addition to the directly matched line, display several previous and next lines (the -Context argument)

The simplest PowerShell equivalent to grep is Select-String. However, it is difficult to use it in PowerShell scripts.
#Powershell grep windows#
On the Windows Command Line (CMD), the equivalent to grep is findstr. In this article, we’ll take a look at the equivalents of the grep command in Windows PowerShell. Select-String Cmdlet Select-String checks for the initial match in each line by default, and then it shows the line number, file name, and text belonging to the matched line. So in this scenario, we have the Select-String cmdlet in PowerShell. By Using grep you can easily find and filter the output returned by the previous command in the pipeline. The grep utility lets users find text using various parameters however, it is not available in Windows. The grep command is widely used on Linux to parse files and shell output.
