title: PowerShell: Export each line of CSV to individual file
body: <p>I have a tab-delimited CSV which needs to be broken apart into separate CSVs for each row, with the filename being [FirstName] [LastName].csv. </p>
  
  <p>Additionally, I need to manipulate some data, specifically remove the dashes from the SSN. </p>
  
  <p><strong>Sample Content</strong></p>
  
  <pre><code>FirstName  LastName   SSN
  =========  =========  ======
  Albert     Abernathy  111-11-1111
  Billy      Barty      222-22-2222
  Chris      Canton     333-33-3333
  </code></pre>
  
  <p><strong>Desired output</strong> (Filename | CSV Content)</p>
  
  <pre><code>Albert Abernathy.csv | Albert     Abernathy  111111111
  Billy Barty.csv      | Billy      Barty      222222222
  Chris Canton.csv     | Chris      Canton     333333333
  </code></pre>
  
  <p>Here's what I have so far.</p>
  
  <pre><code>$csvFile = "C:\Test\List.csv"
  $folderPath = Split-Path -Parent $csvFile
  $csv = Import-Csv -Path $csvFile -Delimiter "`t" 
  | Where-Object {$_.LastName -ne ""} ##Prevent importing blank lines
  | Foreach-Object {$_.SSN -Replace "-","" } ##Remove dashes from SSN
  foreach ($line in $csv)
  {   $saveName = $line.FirstName + " " + $line.LastName + ".csv"
      Export-Csv -Delimiter "`t" -Path $folderPath\$saveName
  }
  </code></pre>
  
  <p>So far, all of this runs, but it just pauses after the final clause and nothing happens. As I cobbled this together from Google searches, I'm certain my syntax needs work, so I'd be grateful if someone could point me in the right direction</p>
  
  <p><strong>--Update--</strong></p>
  
  <p>Credit to alroc for pointing me in the right direction, and mjolinor for providing an invaluable REPLACE syntax I could not find anywhere else. Here's my final script.</p>
  
  <pre><code>$csvFile = "C:\Test\List.csv"
  $folderPath = Split-Path -Parent $csvFile
  
  ### filter out blank records and delete dashes from SSN ###
  (Get-Content $csvFile | 
      Select-Object |
      Where-Object {$_.LastName -ne ""}) | #Rows with non-blank last name
      Foreach-Object {$_ -Replace '(\d{3})-(\d{2})-(\d{4})','$1$2$3'} |
      Set-Content $csvFile
  
  ### import csv data and export file for each row ###
  $csv = Import-Csv -Path $csvFile -Delimiter "`t"
  foreach ($row in $csv) {
      $outPath = Join-Path -Path $folderPath -ChildPath $($row.FirstName + " " + $row.LastName + ".csv");
      $row | Select-Object | 
          ConvertTo-Csv -NoTypeInformation -Delimiter "`t" | 
          Foreach-Object {$_.Replace('"','')} | #Delete quotes around each "cell"
          Out-File $outPath
      (Get-Content $outPath | 
          Select-Object -Skip 1) | #Delete header
          Set-Content $outPath
  }
  </code></pre>