[PowerShell] Batch My CSV File

This was a simple PowerShell script I knocked up to batch some of my very large CSVs down. Save it as say batchMyCsv.ps1 and run it like in:

.\batchMyCsv.ps1 -FileName .\verybig.csv -Batch 1000

The Script

Param(
  [Parameter(Mandatory=$true)][String]$FileName,
  [Int]$Batch = 1000
)

Write-Host ("Getting file $FileName")
[System.Array]$csvContent = Get-Content $FileName
Write-Host ("Splitting $FileName into batches of $Batch")
[String]$FileWoExt = $FileName.split(".")[-2].replace("\","")
Write-Host ("Files will be named " + $FileWoExt + ".X.csv")
[String]$FirstLine = $csvContent[0]
Write-Host ("Common first line: $FirstLine")

$outFile = @()
$counter = 0
$fileCount = 1
$csvContent | Foreach{
  $outFile  += $_
  $counter++
  If($counter -eq $Batch){
    $NewFileName = $FileWoExt + "." + [String]$fileCount + ".csv"
    $outFile | Set-Content $NewFileName
    Write-Host ("Created file $NewFileName")
    $counter = 0
    $outFile = @()
    $outFile += $csvContent[0]
    $fileCount++
  }
}

$NewFileName = $FileWoExt + "." + [String]$fileCount + ".csv"
$outFile | Set-Content $NewFileName
Write-Host ("Created file $NewFileName")


If You Work Somewhere Where PowerShell Scripts Are Not Allowed - How Do You Run The Script?

Where I've been working recently, I cannot actually run PowerShell scripts on my desktop. This is not a problem as all you need to do is open up the PowerShell CLI. And type:

Function BatchMyCSV{

Press enter/return and paste the script into your PowerShell.

Press enter/return again and type:

}

Press enter/return again and job done. Then you can just run it in PowerShell as a function:

> BatchMyCSV -FileName YOURFILENAME

Comments