Counting Log Activity

by Apr 2, 2012

Did you know that Group-Object can analyze text-based log files for you? Here's sample code that tells you how many log entries on a given day a log file contains:

Get-Content $env:windir\windowsupdate.log | 
  Group-Object { $_.SubString(0,10) } -NoElement |
  Sort-Object Count -Descending |
  Select-Object Count, Name

The trick is to submit a script block to Group-Object that extracts the piece of information you want to use for grouping. In the file windowsupdate.log, the first ten characters represent the date on which the line was written to the file.

Twitter This Tip! ReTweet this Tip!