header image

Select-String confusion

Posted by: | January 5, 2013 | 1 Comment |

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

under: PowerShell Basics

1 Comment

  1. By: Eunice on January 15, 2013 at 4:29 pm      

    That’s interesting more than one way to skin a cat. I use this:gci D:\inetpub\cofd\NEWPWS\Root -include .svn -Recurse -Force | Remove-Item -Recurse –Forcegci D:\inetpub\cofd\NEWPWS\Root -include .svn -Recurse -Force | Remove-Item -Recurse –Force