PowerShell for Windows Admins

Jan 5 2013   7:10AM GMT

Select-String confusion

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I have seen a lot of confusion recently over the use of Select-String.

One mis-conception is that you need to use Get-Content to pipe the file contents into Select-String.  Not so. Select-String will read the file for you.

If you just want to scan the files in a single folder to find a specific string then Select-String can do the work for you

Select-String -Path C:\Test\*.txt -Pattern "trial" –SimpleMatch

If you need to work through a folder structure add get-ChildItem to the pipeline

Get-ChildItem -Path C:\Test -Filter *.txt -Recurse |
Select-String -Pattern "trial" –SimpleMatch

One line of PowerShell gives you a very powerful way of filtering the files recursively and testing their contents for a given string

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: