Search for String of text from specific file types

Welcome Forums General PowerShell Q&A Search for String of text from specific file types

This topic contains 3 replies, has 4 voices, and was last updated by

js
 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #119149

    Participant
    Points: -9
    Rank: Member

    I have a PS question and forgive me as I am relatively new to PS. If I wanted to utilize PS to search a system for all files with a specific file type such as *.text as an example and search those files for a specific string of characters and pull any "True" or "False" statements for if the strings were found within each file located.  What would be the best way to accomplish this.

  • #119169

    Participant
    Points: 261
    Helping Hand
    Rank: Contributor

    Get-ChildItem und Select-String are your friends. You should carefully read the help including the examples and you're almost done. 😉

  • #119196
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor
    ls -r *.txt | select-string foobar
    
  • #119218

    Participant
    Points: 35
    Rank: Member

    If you want to just display files under a certain path (recursively), and show only the files that match without including duplicates, then something like this will should work for you.  The reason for the "Sort-Object -Unique" part has to do with matching multiple lines in the same file.

    #Simple Example
    $FileType = '*.ps1'
    $SearchPath = './'
    $SearchPattern = 'function'
    Get-ChildItem -Include $FileType -Recurse -Path $SearchPath | Select-String -Pattern $SearchPattern | Sort-Object -Unique -Property Path | Select-Object Path

    Really, this would be better served as a function or advanced function, especially if you truly need to know which files return 'False' since they do not contain the pattern.  I'm not sure what your exact use case is, but conceptually, you may want to start and experiment with this pattern.  If this falls short, then post how it does and someone may be able to help.

You must be logged in to reply to this topic.