Searching log files the easy way

Want to search through, say, a directory full of log files for a specific message? PowerShell to the rescue!

PS C:\> get-item *.log | Select-String -Pattern "Race Condition"

12-19-2017 02.44.02 PM.log:93413:SIP/2.0 200 Race Condition

Subfolders too:

PS C:\> Get-ChildItem c:\<Path>\*.log -Recurse | Select-String -Pattern "Race Condition"

Ongoing Support\20171011-SIP503s\Logs-20171219\12-19-2017 02.44.02 PM.log:93413:SIP/2.0 200 Race Condition

Want to make the output a little prettier?

PS C:\> get-item *.log | Select-String -Pattern "Race Condition" | ft -auto

IgnoreCase LineNumber Line                       Filename                   Path
---------- ---------- ----                       --------                   ----
      True      93413 SIP/2.0 200 Race Condition 12-19-2017 02.44.02 PM.log C:\My\Customers\Contoso\Ongoing ...

Or maybe drop some of the columns, and stop the long path from being truncated:

PS C:\> Get-ChildItem *.log -Recurse | Select-String -Pattern "Race Condition" | ft linenumber,filename,path -wrap

LineNumber Filename                   Path
---------- --------                   ----
     93413 12-19-2017 02.44.02 PM.log C:\My\Customers\Contoso\Ongoing
                                      Support\20171011-SIP503s\Logs-20171219\12-19-2017 02.44.02 PM.log

I hope you can use these examples to make your log trawling exercises a little more fulfilling. “Select-String” supports the use of regular expressions as well, so it’s possible to make these queries a lot more complicated if required.

 

– Greig.

Leave a Reply

Your email address will not be published. Required fields are marked *

... and please just confirm for me that you're not a bot first: Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.