PowerShell Script to Split Large Text File by Line

I had a large SQL file (73MB) that bogged down evey text editor I had available to me on my Windows machine. I knew I needed a particular line from the text file. So based on a Stackoverflow answer and created a split.ps1 PowerShell script:

$Path = "C:\backup"
$InputFile = (Join-Path $Path "backup.sql")
$Reader = New-Object System.IO.StreamReader($InputFile)
$i = 1
While (($Line = $Reader.ReadLine()) -ne $null) {
    $OutputFile = $i.ToString() + ".txt"

    Add-Content (Join-Path $Path $OutputFile) $Line

Running the script gave me a file for each line.


No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *