Context. You have a log directory with hundreds of log files, you need to look for a specific string, but you don’t know which file it resides in.
With PowerShell we can restrict things down in two ways.
- If we roughly know when the log entry was done, we can constrain on time.
- We can then use Select String to filter further.
$daysToCheck = $(get-date).AddDays(-2)
-2 in this case indicates I want to find files that were modified at most 2 days ago. These means from right now, go back a max of 2 days.
Get-ChildItem -Recurse | ?{$_.LastWriteTime -gt $daysToCheck} | Select-String "String to Search for" -list | Select Path
In this example it’ll search the current working directory as it was not defined in the first command call. the list operation is important as to only list the file the string was found in once, else the file path will be listed for every instance the string is found within the file.
This will list all the files contain the string in question. What you wish to do with this is list is on you. However you at least now know where to look further for more information on whatever it is you might be looking for.
Hope this helps someone.