Categories

Select-String confusion

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

One Response to Select-String confusion

  • Eunice says:

    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

Leave a Reply