1

Team -- I have a snippet of code that works as designed. It will scan all the files within a folder hierarchy for a particular word and then return the de-duped file path of the files where all instance of the word were found.

$properties = @(
    'Path'
)

Get-ChildItem -Path \\Server_Name\Folder_Name\* -recurse |  
Select-String -Pattern ‘Hello’ | 
Select-Object $properties -Unique |
Export-Csv \\Server_Name\Folder_Name\File_Name.csv -NoTypeInformation

I'd like to

  1. Expand this code to be able to search for multiple words at once. So all cases where 'Hello' OR 'Hola' are found... and potentially an entire list of words if possible.
  2. Have the code return not only the file path but the word that tripped it ... with multiple lines for the same path if both words tripped it

I've found some article talking about doing multiple word searches using methods like:

  where { $_ | Select-String -Pattern 'Hello' } |
  where { $_ | Select-String -Pattern 'Hola' } |

OR

Select-String -Pattern ‘(Hello.Hola)|(Hola.Hello)’ 

These codes will run ... but return no data is returned in the output file ... it just blank with the header 'Path'.

I'm missing something obvious ... anyone spare a fresh set of eyes?

MS

1
  • 1
    How about -Pattern 'Hello|Hola'? Commented May 20, 2021 at 21:37

1 Answer 1

7
Get-ChildItem -Path \\Server_Name\Folder_Name\* -recurse |  
  Select-String -Pattern 'Hello', 'Hola' | 
    Select-Object Path, Pattern |
      Export-Csv \\Server_Name\Folder_Name\File_Name.csv -NoTypeInformation

Note:

  • If a given matching line matches multiple patterns, only the first matching pattern is reported for it, in input order.

  • While adding -AllMatches normally finds all matches on a line, as of PowerShell 7.2.x this doesn't work as expected with multiple patterns passed to -Pattern, with respect to the matches reported in the .Matches property - see GitHub issue #7765.

    • Similarly, the .Pattern property doesn't then reflect the (potentially) multiple patterns that matched; and, in fact, this isn't even possible at the moment, given that the .Pattern property is a [string]-typed member of [Microsoft.PowerShell.Commands.MatchInfo] and therefore cannot reflect multiple patterns.
Sign up to request clarification or add additional context in comments.

5 Comments

This snippet won't work according to OP's second condition: "with multiple lines for the same path if both words tripped it". Select-String only ever reports the first matching Pattern listed in the array, the MatchInfo object it returns doesn't have the ability to store multiple match patterns at once. See the following Github issue: github.com/PowerShell/PowerShell/issues/7765"
@skaravos, it was I who created the GitHub issue you link to, and it doesn't apply here, given that -AllMatches isn't involved. "with multiple lines for the same path if both words tripped it" doesn't necessarily imply that if multiple patterns match on the same line that they should be listed individually (perhaps in the input date they never appear together on any single line), and the fact that the OP accepted this answer suggests that that wasn't a requirement.
@skaravos, I've added a clarification to the answer.
Can't believe I linked you to your own GitHub issue... real facepalm moment. Great edit though!
@skaravos :) glad to hear it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.