Pages

June 4, 2012

PowerShell Tips & Tricks (Select-String)

Are you familiar with grep in the Linux & Unix world and are looking for an equivalent Command in PowerShell to accomplish the same task? Well look no further than the Select-String Command-Let.

Select-String can be used to search files for folders for a particular string or regular expression.

Example:
To search all text files in the current directory to find the pattern Dog.
Select-string -path *.txt -pattern "Dog"

The output of this command will contain "FileName.txt:LineNumber:Pattern"

Now Lets say you would like to just return the file name that the pattern was found in so you can pipe that to another command. Use the following command:

Select-string -path *.txt -pattern "Dog" |  Format-list name

No comments:

Post a Comment