PowerShell to split a large disk/log file

I had a huge 550 MB file, I wanted to split it into 55 or so files of 10 MB each.

The one improvement I would make to this next time is to make the suffix a 3 digit number so when the files sort by name they would be in numerical order (currently _1 and _10 sort together, _2 and _20 and so on).

$ErrorActionPreference = "Stop"
$upperBound = 10MB # calculated by Powershell
$ext = "log"
$rootName = "c:\ProgramData\ssh\logs\sshd_log_Split_"

$reader = new-object System.IO.StreamReader("c:\ProgramData\ssh\logs\sshd_Archive.log")
$count = 1
$fileName = "{0}{1}.{2}" -f ($rootName, $count, $ext)
while(($line = $reader.ReadLine()) -ne $null)
    Add-Content -path $fileName -value $line
    if((Get-ChildItem -path $fileName).Length -ge $upperBound)
        $fileName = "{0}{1}.{2}" -f ($rootName, $count, $ext)


The above is based some minor modifications to a sample found here: https://stackoverflow.com/questions/1001776/how-can-i-split-a-text-file-using-powershell

Leave a Reply

Recent Posts




All Rights Reserved Theme by 404 THEME.