Takes a text file and splits it up into subfiles of a specified number of lines if it exceeds a certain size threshold.  There is some basic error catching included.  It also reports on the time of execution.  I used this to determine the speed difference between using the .NET method vs Out-File.  I found that the .NET method was quite a bit faster, so that is the method I used in the script.  This script could probably be converted into a function for re-usability fairly simply.  If you are going to use it as is, make sure to change the variables to values that fit your needs.

See details here: http://ryandevries.com/index.php/2015/04/30/split-text-file-based-on-size/

 

PowerShell
Edit|Remove
<#  
.SYNOPSIS  
    Breaks text-based file over specified size into files of specified length and stores them in a subdirectory 
.DESCRIPTION  
    Purges all files in $destpath, then splits file up into $numlines chunks and stores in $destpath if the file is over $maxsize 
.NOTES  
    Author     : Ryan DeVries 
    Updated    : 2015-04-30 
#>  
 
#-----------------------------------------------# 
# VARIABLES 
#-----------------------------------------------# 
 
$sourcepath = 'C:\Temp' 
$sourcefile = 'srcfilename.sql' 
$destfile   = 'dstfilename' 
$destpath   = 'C:\Temp\dst' 
$maxsize    = 5000KB 
$numlines   = 10000 
 
#-----------------------------------------------# 
# START 
#-----------------------------------------------# 
$start = get-date 
try { 
    remove-item "$destpath\*" -ErrorAction Stop 
    $file = get-item "$sourcepath\$sourcefile" -ErrorAction Stop 
    if($file.length -gt $maxsize) { 
        $count = 1 
        get-content "$sourcepath\$sourcefile" -ReadCount $numlines |  
        foreach-object {  
            $destfilename = "{0}{1}.{2}" -f ($destfile$count"sql") 
            [system.io.file]::WriteAllLines("$destpath\$destfilename"$_) 
            $count++ 
        } 
    } 
} 
catch [system.exception] { 
    write-error $_.Exception.Message -TargetObject $_.Exception.ItemName 
    exit 1 
} 
$end = get-date 
(New-TimeSpan -start $start -end $end).TotalSeconds 
#-----------------------------------------------#   
# END 
#-----------------------------------------------#