Wednesday, February 17, 2010

A Powershell GREP

Here's a handy one for doing a GREP in PowerShell

Example 1
Get-ChildItem -include *.txt -recurse | Select-String "Hello"

Example 2
Get-ChildItem -include *.txt -recurse | Select-String "Hello" | Format-Table

Example 3
Get-ChildItem -include *.txt -recurse | Select-String "Hello" | Format-Table -Property Path, LineNumber, Line -Autosize

This gets a collection of all "txt" files in the current directory and its sub directories, passes it to the "Select-String" which opens the file and look for the pattern "Hello".

The Format-Table option allows you to display the results in a more readable format. You can also use "Format-List"

In example 3 I added the AutoSize property so the Path info wouldn't get truncated.

No comments:

Post a Comment