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

4 months, 4 weeks ago.

  • Author
  • #119149

    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

    Points: 1,166
    Helping Hand
    Rank: Community Hero

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

  • #119196

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

    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.

The topic ‘Search for String of text from specific file types’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort