Parsing log files with Powershell

I found myself wanting to parsing a log file to find out which domain was getting the most newsletters. There are a variety of ways you can do this. Typically I would use Excel but there were more than 65K lines to import so I had to use something different. For kicks I did it in Powershell and here is how I did it in three lines. The log file is a tab delimited file without a header line. The field we are going to count is called, “RecipientDomain”.

 $header = "ServerFQDN","ServerDomain","IPAddress","MailTime","ClientDomain","RecipientDomain","Sender","Recipient","MessageID","Status","User","Size","ClientFQDN" 
$a = import-csv smtp-201007060000.csv -delimiter `t -header $header | where-object {($_.Status -eq "RECV=OK") } 
$a | group-object -property RecipientDomain -noelement | sort -property @{Expression="Count";Descending=$true}, @{Expression="Name";Descending=$false} | Select-object -First 20