title: Powershell to Break up CSV by Number of Row
body: <p>So I am now tasked with getting constant reports that are more than 1 Million lines long.</p>
  
  <p>My last question did not explain all things so I'm tryin got do a better question.</p>
  
  <p>I'm getting a dozen + daily reports that are coming in as CSV files. I don't know what the headers are or anything like that as I get them.</p>
  
  <p>They are huge. I cant open in excel.</p>
  
  <p>I wanted to basically break them up into the same report, just each report maybe 100,000 lines long.</p>
  
  <p>The code I wrote below does not work as I keep getting a </p>
  
  <pre><code>Exception of type 'System.OutOfMemoryException' was thrown.
  </code></pre>
  
  <p>I am guessing I need a better way to do this. </p>
  
  <p>I just need this file broken down to a more manageable size.
  It does not matter how long it takes as I can run it over night.</p>
  
  <p>I found this on the internet, and I tried to manipulate it, but I cant get it to work.</p>
  
  <pre><code>$PSScriptRoot
  
  write-host $PSScriptRoot
  
  $loc = $PSScriptRoot
  
  $location = $loc
  
  # how many rows per CSV?
  $rowsMax = 10000; 
  
  # Get all CSV under current folder
  $allCSVs = Get-ChildItem "$location\Split.csv"
  
  
  # Read and split all of them
  $allCSVs | ForEach-Object {
      Write-Host $_.Name;
      $content = Import-Csv "$location\Split.csv"
      $insertLocation = ($_.Name.Length - 4);
      for($i=1; $i -le $content.length ;$i+=$rowsMax){
      $newName = $_.Name.Insert($insertLocation, "splitted_"+$i)
      $content|select -first $i|select -last $rowsMax | convertto-csv -NoTypeInformation | % { $_ -replace '"', ""} | out-file $location\$newName -fo -en ascii
      }
  }
  </code></pre>