Search code examples
powershellpowershell-cmdlet

powershell Parsing for multiple keywords and sending output to a text file


I'm trying to write a powershell cmdlet to find multiple words in lines in file. Example. I need to parse "word1", "word2", "word3" are in the same line of a file. I'm doing something wrong because I tried this with no success:

(gci -File -Filter FileName | Select-String -SimpleMatch word1, word2,word3) > outputFileName.txt

where FileName = name of file, outputFileName = generated file from my search of the three words. Thank you.


Solution

  • Select-String doesn't have any combination operator that I can think of. If your words were always in that order, then you could do -Pattern 'word1.*word2.*word3' as your match, but if they could be in any order that would get complex very quickly. Instead, I'd look at

    .. | Select-String 'word1' | Select-String 'word2' | Select-String 'Word3'
    

    so, all the lines which match word1. Out of those, the ones which match word2 somewhere. Out of that even smaller result, the ones which also match word3.