Powershell – Combine Log Files

Gets the files inside of a folder, orders them by last write time, combines the contents, then outputs to a new file.  Used when I had multiple rolling log files that I wanted to combine so I could read the entries easier.

# Set the path to the folder containing the files you want to combine
$folderPath = "Path-to\Folder"

# Get all files in the folder
$files = Get-ChildItem -Path $folderPath -File

# Sort files by last modified time, oldest to newest
$files = $files | Sort-Object LastWriteTime

# Create an empty array to hold file contents
$fileContents = @()

# Loop through each file and add its contents to the array
foreach ($file in $files) {
    $content = Get-Content $file.FullName
    $fileContents += $content
}

# Combine all file contents into one string
$combinedContents = $fileContents -join "`r`n"

# Write combined contents to a new file
Set-Content -Path "$folderPath\combined.txt" -Value $combinedContents