Import-CsvToSql: Super Fast CSV to SQL Server Import PowerShell Module

A while back, I wrote about High-Performance Techniques for Importing CSV to SQL Server using PowerShell. Earlier today, I released a PowerShell module, CsvSqlImport.psm1, which puts that post into practice.

Using this module, it’s possible to import over 90,000 records a second in optimized environments. In addition to being speedy, it’s also straightforward to use and can be easily scheduled for ongoing imports.

Turbo charged

Check out how quick 1,000,000 rows can be imported when the -Turbo switch is used:


Why isn’t Turbo used by default? Well there’s very little error management (that slows things down) so it may error out when other methods (default and -Safe) may not.

Why not just use the Import/Export Wizard?

Because it stopped being easy for me awhile back (and it’s purely GUI, and requires additional software and it can’t import multiple files at once, and so on.)

Remember when importing CSVs into SQL Server using dtswizard didn’t require any conversions or fanciness? You’d just select your file and your delimiter, it would create the table for you using varchar(255) across the board, and plink! You’d almost always get the “Successfully copied 1 table(s) from Flat File to Microsoft SQL Server.”


Then, the Import Wizard became more powerful but also more complicated, and quick and dirty imports suddenly took more work. For instance, whether I allow the Wizard to create a new table, or try to map to an existing one, my imports in SQL Server 2005 and above nearly always go like this:


Noooo! What is all this mapping? I just want it to work then I can mess with my datatypes later. *Next*


“Well, damn. I should just write a PowerShell function to do this for me.” And this is basically how Import-CsvToSql came about.


Eventually, I’ll have support for a GUI, too. I created one, but got too caught up in best practices, and decided to put it off while until I have time to learn more about WPF (which I love, btw.)

Like many PowerShell functions, Import-CsvtoSql supports the -First parameter. As I mentioned earlier, it can even import multiple files at once, so long as they’re similarly formatted. It also allows you to query the CSV file for subsets of data (think select address, cost from csv where State = ‘Louisiana’), and it supports all the options within SqlBulkCopy like Table Lock, Check Constraints, Fire Triggers, Keep Identity, and Keep Nulls.

Most of my imports have been pretty basic, but I did add in features that I thought others would appreciate. If you have any more you’d like me to add, let me know. Oh, also, I did my best to comment the hell out of the code, so if you’re wondering how it works, it should be easy to figure out.

Give the script a try and let me know how you like it. You only need PowerShell 3.0 and above. No additional software is required. If you run into any issues, send a portion of your CSV file to me at [email protected], and I’ll take a look to see what’s going on.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in PowerShell, SQL Server
One comment on “Import-CsvToSql: Super Fast CSV to SQL Server Import PowerShell Module
  1. This is really cool. The import/export wizard has been a PITA for so long now.

Leave a Reply

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


Migrating SQL Server?

dbatools is an awesome PowerShell module that helps you migrate entire instances with a single command.

Available from and github

SqlServer Needs You

SqlServer now has a dedicated engineer and Microsoft is asking for our input!

Upvote priorities and cmdlets now


Chrissy LeMaire

Brandon Abshire
View Brandon Abshire, MCDBA's profile on LinkedIn


Chrissy has been awarded the Microsoft MVP for her work in the PowerShell community.

Join us!

Belgian PowerShell
User Group

  SQL PASS PowerShell
Virtual User Group


Upvotes Needed

Help persuade Microsoft to open source SQL Server's PowerShell module, SQLPS.

Upvote now on Microsoft Connect