Search code examples
sql-serverpowershellcsvpowershell-3.0powershell-4.0

Can you import .CSV data into SQL Server using commands from Powershell 3 or 4?


Is there a Powershell script for PowerShell version 3 or version 4 that allows you to import values from a .CSV file into a SQL table?

  1. I'm trying to figure out whether I can script a PowerShell job to import data, using enterprise Servers running older versions of PowerShell (It would be preferred if this can be done without importing any additional modules)

  2. (Nice to have, but not necessary) Also interested to know if there's a way to report the success or failure of that job, to a log file?

This is the script I've tested in PowerShell 5, just can't use this on my older servers:

$database = 'DATABASE';
$server = 'SERVER';
$table = 'TABLE';
$MyData = import-csv \\TESTPATH\licenses_v2.csv -delimiter '|' | 
select-object FirstName,LastName,Department,Title;
Write-DbaDataTable -sqlinstance $server -database $database -table $table -inputobject $MyData -KeepNulls;  

Solution

  • The answer is yes, I just need to install the DBATools module.