Let’s assume a script has written multiple log files in a folder, all with extension *.log. You would like to consolidate them into one big file. Here is a simple approach:

$OutPath = "$env:temp\summary.log"

Get-Content -Path "C:\Users\tobwe\Documents\ScriptOutput\*.log" |
  Set-Content $OutPath

Invoke-Item -Path $OutPath

However, this approach wouldn’t give a lot of control to you: all files would need to reside in the same folder, have the same file extension, and you have no control over the order in which they are consolidated.

A more versatile approach would look like this:

$OutPath = "$env:temp\summary.log"

Get-ChildItem -Path "C:\Users\demouser\Documents\Scripts\*.log" -Recurse -File |
    Sort-Object -Property LastWriteTime -Descending |
    Get-Content |
    Set-Content $OutPath

Invoke-Item -Path $OutPath

It lets you use all the flexibility of Get-ChildItem, and you get the chance to sort files before you read their content. This way, the summary keeps the order, and the latest log information always appears on top of the summary log file.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Anonymous