The scenario is that we want an overview of our Windows log files. Time is short so we just want a list of the files which contain the most ‘Errors’. We will use PowerShell to achieve this.
Incidentally, if you try this script for real you will be shocked at how many errors there are in your Windows operating system.
Clear-Host $Directory = "C:\Windows\" $Phrase = "Error" $Files = Get-Childitem $Directory -recurse -Include *.log -ErrorAction SilentlyContinue $Files | Select-String $Phrase -ErrorAction SilentlyContinue | Group-Object filename | Sort-Object count -descending
The secret of unravelling how it bolts together the elements is to look for the (|) pipes. The vertical bar (|) is PowerShell‘s signature tune, it pipes the output of the first command into the second phrase, and then output of the second phase into the third part.
In this instance, once ‘Get-ChildItem’ supplies its list of log files, Select-String filters this stream, then passes it along the chain for grouping and sorting.