Select-String powershell advice

I found a usefulPowerShell script online (full code below) which helped me review an enormous txt file (log file) for rows containing a key word phrase and output just those rows to a txt file. What I could do with is a way of amending the $pattern row below:

$pattern = ‘searchterm’

Open in new window



..to search another text file I have created which contains for a number of search terms I have put together, rather than me having to run the command for every search phrase I need to check individually.

full code:

$inFile = '\\server\share\directoruy\file.txt'
$outFile = '\\server\share\directoruy\hits.txt'
$pattern = ‘searchterm’
Select-String -Path $inFile -SimpleMatch $pattern | Select-Object -ExpandProperty Line | Set-Content -Path $outFile

Open in new window


for info the format of the text file I have created for using is just in a row by row format, e.g

searchphrase1
searchphrase2
searchphrase3
LVL 4
pma111Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Use -Pattern instead of -SimpleMatch, and use a regex "or". Since you're currently using SimpleMatch, the following will automatically escape all characters in the text file that have a special meaning in regex. For more complex patterns than straight-forward strings, this would need to be adjusted:
$inFile = '\\server\share\directoruy\file.txt'
$outFile = '\\server\share\directoruy\hits.txt'
$patternFile = 'C:\Temp\patternlist.txt'
$pattern = (Get-Content -Path $patternFile | ForEach-Object {[regex]::Escape($_)}) -join '|'
Write-Host "Searching with pattern '$($pattern)'"
Select-String -Path $inFile -Pattern $pattern | Select-Object -ExpandProperty Line | Set-Content -Path $outFile

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.