It can be very time-consuming and tedious to sift through many files. Unless you’re a fan of working over a weekend and opening file after file while being slumped in a chair, then I would suggest making use of the information from today’s PowerShell blog post!
Consider a directory, “C:\Temp” with many text files created. Each of the files has random text data inside. We’re looking for only the files that contain one particular string. Additionally, since we don’t know how many matches we are going to find, we’re going to create an array to store the found matches. In order to search for strings or string patterns, we’re going to use the cmdlet Select-String.
Here’s the breakdown
This will search the directory $Path for any items that include “.txt” in their names and that are not directories.
For every match it finds, it will check the contents of the match using Get-Content and verify any matches with $Text by using Select-String. If it finds a match, it puts the full name of the match into the $PathArray array.
There you have it.
Exporting results to file
If you want to export that all to a file instead of on the screen, then simply pipe it into an Out-File cmdlet. For example:
Searching through all subfolders
If you want to search all subfolders for additional *.txt files as well, then add “-recurse” to the original Get-ChildItem command: