{"id":11951,"date":"2011-11-28T00:01:00","date_gmt":"2011-11-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/28\/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell\/"},"modified":"2011-11-28T00:01:00","modified_gmt":"2011-11-28T00:01:00","slug":"four-easy-ways-to-import-csv-files-to-sql-server-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell\/","title":{"rendered":"Four Easy Ways to Import CSV Files to SQL Server with PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Learn four easy ways to use Windows PowerShell to import CSV files into SQL Server.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. &nbsp;I was chatting this week with Microsoft PowerShell MVP, Chad Miller, about the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/csv+and+other+delimited+files\/\" target=\"_blank\">series of blogs I recently wrote about using CSV files<\/a>. He thought a helpful addition to the posts would be to talk about importing CSV files into a SQL Server. I most heartily agreed. Welcome to Guest Blogger Week. We will start off the week with a bang-up article by Chad Miller. Chad has previously written <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/guest+blogger\/chad+miller\/\" target=\"_blank\">guest blogs<\/a> for the Hey, Scripting Guy! Blog. Here is a little information about Chad:<\/p>\n<p style=\"padding-left: 30px\">Chad Miller is a SQL Server database admin and the senior manager of database administration at Raymond James Financial.&nbsp;In his spare time, he is the project coordinator and developer of&nbsp;the CodePlex project SQL Server PowerShell Extensions (<a href=\"http:\/\/sqlpsx.codeplex.com\/\" target=\"_blank\">SQLPSX<\/a>). Chad leads the <a href=\"http:\/\/powershellgroup.org\/tampa.fl\" target=\"_blank\">Tampa Windows PowerShell User Group<\/a>, and he is a frequent speaker at SQL Saturdays and Code Camps.<\/p>\n<p style=\"padding-left: 30px\">Contact information:<br \/> Blog: <a href=\"http:\/\/sev17.com\/\" target=\"_blank\">Sev17<\/a><br \/> Twitter: <a href=\"https:\/\/twitter.com\/#!\/cmille19\" target=\"_blank\">cmille19<\/a><\/p>\n<h2>Importing CSV files into SQL Server<\/h2>\n<p>Windows PowerShell has built in support for creating CSV files by using the <b>Export-CSV<\/b> cmdlet. However, the creation of a CSV file is usually only a short stop in an overall process that includes loading the file into another system. In this post, we&#8217;ll look at a few scripted-based approaches to import CSV data into SQL Server. <b>Note<\/b>: SQL Server includes a component specifically for data migration called SQL Server Integration Services (SSIS), which is beyond the scope of this article.<\/p>\n<h2>T-SQL BULK INSERT command<\/h2>\n<p>The T-SQL <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188365.aspx\" target=\"_blank\">BULK INSERT <\/a>command is of the easiest ways to import CSV files into SQL Server. The BULK INSERT command requires a few arguments to describe the layout of the CSV file and the location of file. Let&#8217;s look at an example of creating a CSV file by using <b>Export-CSV<\/b>, and then importing the information into a SQL Server table by using BULK INSERT.<\/p>\n<p><strong>Requirements<\/strong><\/p>\n<ul>\n<li>Sysadmin or insert and bulkadmin to SQL Server<\/li>\n<li>Local access to SQL Server<\/li>\n<\/ul>\n<p><strong>Setup<\/strong><\/p>\n<p><strong><\/strong>1. Download the following script: <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/7985b7ef-ed89-4dfd-b02a-433cc4e30894\/\" target=\"_blank\">Invoke-SqlCmd2.ps1<\/a><\/p>\n<p><a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/7985b7ef-ed89-4dfd-b02a-433cc4e30894\/\"><\/a>2. Create a table disk space by copying the following code in SQL Server Management Studio.&nbsp;<\/p>\n<p><b>Note<\/b>: The example uses a database named &#8220;hsg.&#8221;<\/p>\n<p style=\"padding-left: 30px\">CREATE TABLE dbo.diskspace(<\/p>\n<p style=\"padding-left: 30px\">UsageDate datetime,<\/p>\n<p style=\"padding-left: 30px\">SystemName varchar(50),<\/p>\n<p style=\"padding-left: 30px\">Label varchar(50),<\/p>\n<p style=\"padding-left: 30px\">VolumeName varchar(50),<\/p>\n<p style=\"padding-left: 30px\">Size decimal(6,2),<\/p>\n<p style=\"padding-left: 30px\">Free decimal(6,2),<\/p>\n<p style=\"padding-left: 30px\">PrecentFree decimal(5,2)<\/p>\n<p style=\"padding-left: 30px\">)<\/p>\n<p>The following image shows the command in SQL Server Management Studio.<\/p>\n<pre><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8540.HSG-11-28-11-1.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8540.HSG-11-28-11-1.jpg\" alt=\"Image of query\" title=\"Image of query\" \/><\/a><\/pre>\n<pre><\/pre>\n<p>3. Save the following script as Get-DiskSpaceUsage.ps1, which will be used as the demonstration script later in this post.<\/p>\n<p style=\"padding-left: 30px\">param($ComputerName=&#8221;.&#8221;)<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -computername &#8220;$computername&#8221; Win32_Volume -filter &#8220;DriveType=3&#8221; | foreach {<\/p>\n<p style=\"padding-left: 30px\">new-object PSObject -property @{<\/p>\n<p style=\"padding-left: 30px\">UsageDate = $((Get-Date).ToString(&#8220;yyyy-MM-dd&#8221;))<\/p>\n<p style=\"padding-left: 30px\">SystemName = $_.SystemName<\/p>\n<p style=\"padding-left: 30px\">Label = $_.Label<\/p>\n<p style=\"padding-left: 30px\">VolumeName = $_.Name<\/p>\n<p style=\"padding-left: 30px\">Size = $([math]::round(($_.Capacity\/1GB),2))<\/p>\n<p style=\"padding-left: 30px\">Free = $([math]::round(($_.FreeSpace\/1GB),2))<\/p>\n<p style=\"padding-left: 30px\">PercentFree = $([math]::round((([float]$_.FreeSpace\/[float]$_.Capacity) * 100),2))<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">} | Select UsageDate, SystemName, Label, VolumeName, Size, Free, PercentFree<\/p>\n<p>Now we will use the script Get-DiskSpaceUsage.ps1 that I presented earlier. It lists information about disk space, and it stores the information in a CSV file.<\/p>\n<p style=\"padding-left: 30px\"><span class=\"Apple-style-span\">.\/get-diskusage.ps1 | export-csv -Path &#8220;C:\\Users\\Public\\diskspace.csv&#8221; -NoTypeInformation<\/span><\/p>\n<p>The generated CSV file shows that <b>Export-CSV<\/b> includes a text delimiter of double quotes around each field:<\/p>\n<p style=\"padding-left: 30px\">&#8220;UsageDate&#8221;,&#8221;SystemName&#8221;,&#8221;Label&#8221;,&#8221;VolumeName&#8221;,&#8221;Size&#8221;,&#8221;Free&#8221;,&#8221;PercentFree&#8221;<\/p>\n<p style=\"padding-left: 30px\">&#8220;2011-11-20&#8243;,&#8221;WIN7BOOT&#8221;,&#8221;RUNCORE SSD&#8221;,&#8221;D:\\&#8221;,&#8221;59.62&#8243;,&#8221;31.56&#8243;,&#8221;52.93&#8243;<\/p>\n<p style=\"padding-left: 30px\">&#8220;2011-11-20&#8243;,&#8221;WIN7BOOT&#8221;,&#8221;DATA&#8221;,&#8221;E:\\&#8221;,&#8221;297.99&#8243;,&#8221;34.88&#8243;,&#8221;11.7&#8243;<\/p>\n<p style=\"padding-left: 30px\">&#8220;2011-11-20&#8243;,&#8221;WIN7BOOT&#8221;,,&#8221;C:\\&#8221;,&#8221;48&#8243;,&#8221;6.32&#8243;,&#8221;13.17&#8243;<\/p>\n<p style=\"padding-left: 30px\">&#8220;2011-11-20&#8243;,&#8221;WIN7BOOT&#8221;,&#8221;HP_TOOLS&#8221;,&#8221;F:\\&#8221;,&#8221;0.1&#8243;,&#8221;0.09&#8243;,&#8221;96.55&#8243;<\/p>\n<p>Although many programs handle CSV files with text delimiters (including SSIS, Excel, and Access), BULK INSERT does not. To use BULK INSERT without a lot of work, we&#8217;ll need to remove the double quotes. We can use a quick and dirty way of simply replacing all the quotes in the CSV file. In the blog post <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/11\/02\/remove-unwanted-quotation-marks-from-csv-files-by-using-powershell.aspx\" target=\"_blank\">Remove Unwanted Quotation Marks from CSV Files by Using PowerShell<\/a>, the Scripting Guys explains how to remove double quotes. This method can be used for circumstances where you know it won&#8217;t cause problems. How do you know? Well, the data being generated from our <b>Get-DiskspaceUsage<\/b> should never have double quotes or commas in the data. So here&#8217;s the code to remove the double quotes:<\/p>\n<p style=\"padding-left: 30px\">(Get-Content C:\\Users\\Public\\diskspace.csv) | foreach {$_ -replace &#8216;&#8221;&#8216;} | Set-Content C:\\Users\\Public\\diskspace.csv<\/p>\n<p style=\"padding-left: 30px\">UsageDate,SystemName,Label,VolumeName,Size,Free,PercentFree<\/p>\n<p style=\"padding-left: 30px\">2011-11-20,WIN7BOOT,RUNCORE SSD,D:\\,59.62,31.56,52.93<\/p>\n<p style=\"padding-left: 30px\">2011-11-20,WIN7BOOT,DATA,E:\\,297.99,34.88,11.7<\/p>\n<p style=\"padding-left: 30px\">2011-11-20,WIN7BOOT,,C:\\,48,6.32,13.17<\/p>\n<p style=\"padding-left: 30px\">2011-11-20,WIN7BOOT,HP_TOOLS,F:\\,0.1,0.09,96.55<\/p>\n<p>Now we are ready to import the CSV file as follows:<\/p>\n<p style=\"padding-left: 30px\">. .\\Invoke-SqlCmd2.ps1<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$query = @&#8221;<\/p>\n<p style=\"padding-left: 30px\">BULK INSERT hsg.dbo.diskspace FROM &#8216;C:\\Users\\Public\\diskspace.csv&#8217;<\/p>\n<p style=\"padding-left: 30px\">WITH (FIRSTROW = 2, FIELDTERMINATOR = &#8216;,&#8217;, ROWTERMINATOR = &#8216;\\n&#8217;)<\/p>\n<p style=\"padding-left: 30px\">&#8220;@<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Invoke-SqlCmd2 -ServerInstance &#8220;$env:computername\\sql1&#8221; -Database hsg -Query $query<\/p>\n<p>The following data shows that our CSV file was successfully imported.<\/p>\n<table border=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<p align=\"center\"><b>UsageDate<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>System<br \/> Name<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Label<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Volume<br \/> Name<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Size<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Free<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Percent<br \/> Free<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>11\/20\/2011 12:00:00 AM<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td>\n<p>RUNCORE SSD<\/p>\n<\/td>\n<td>\n<p>D:\\<\/p>\n<\/td>\n<td>\n<p>59.62<\/p>\n<\/td>\n<td>\n<p>31.56<\/p>\n<\/td>\n<td>\n<p>52.93<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>11\/20\/2011 12:00:00 AM<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td>\n<p>DATA<\/p>\n<\/td>\n<td>\n<p>E:\\<\/p>\n<\/td>\n<td>\n<p>297.99<\/p>\n<\/td>\n<td>\n<p>34.88<\/p>\n<\/td>\n<td>\n<p>11.70<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>11\/20\/2011 12:00:00 AM<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td><\/td>\n<td>\n<p>C:\\<\/p>\n<\/td>\n<td>\n<p>48.00<\/p>\n<\/td>\n<td>\n<p>6.32<\/p>\n<\/td>\n<td>\n<p>13.17<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>11\/20\/2011 12:00:00 AM<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td>\n<p>HP_TOOLS<\/p>\n<\/td>\n<td>\n<p>F:\\<\/p>\n<\/td>\n<td>\n<p>0.10<\/p>\n<\/td>\n<td>\n<p>0.09<\/p>\n<\/td>\n<td>\n<p>96.55<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>BULK INSERT works reasonably well, and it is very simple. However, there are some drawbacks, including:<\/p>\n<ul>\n<li>You need elevated permissions on SQL Server.<\/li>\n<li>BULK INSERT doesn&#8217;t easily understand text delimiters.<\/li>\n<li>Using the UNC path to files requires an additional setup, as documented under <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188365.aspx\" target=\"_blank\">Permissions on the BULK INSERT site<\/a>.<\/li>\n<\/ul>\n<p>For these reasons, let&#8217;s look at some alternate approaches.<\/p>\n<h2>Before there was Windows PowerShell, there was LogParser<\/h2>\n<p>LogParser is a command-line tool and scripting component that was originally released by Microsoft in the IIS&nbsp;6.0 Resource Kit. LogParser provides query access to different text-based files and output capability to various data sources including SQL Server. Even though this little tool hasn&#8217;t been updated since 2005, it has some nice features for loading CSV files into SQL Server.<\/p>\n<p><strong>Setup<\/strong><\/p>\n<p>Download and install <a href=\"http:\/\/www.microsoft.com\/download\/en\/details.aspx?displaylang=en&amp;id=24659\" target=\"_blank\">LogParser 2.2<\/a>.<\/p>\n<p>LogParser can do a few things that we couldn&#8217;t easily do by using BULK INSERT, including:<\/p>\n<ul>\n<li>Automatically create a table based on the CSV layout<\/li>\n<li>Handle the text delimiter of double quotes<\/li>\n<\/ul>\n<p><b>Note<\/b>: CSV files do not need to be local.<\/p>\n<p><strong>Using LogParser<\/strong><\/p>\n<p>You can use the LogParser command-line tool or a COM-based scripting interface. Let&#8217;s look at examples of both.<\/p>\n<p><strong>LogParser command-line tool<\/strong><\/p>\n<p>LogParser requires some special handling, which is why we use <b>Start-Process<\/b>. Some switches and arguments are difficult to work with when running directly in Windows PowerShell. Also Windows Powershell_ISE will not display output from LogParser that are run via the command-line tool. Here is the syntax for running a command to generate and load a CSV file:<\/p>\n<p style=\"padding-left: 30px\">.\/get-diskspaceusage.ps1 | export-csv -Path &#8220;C:\\Users\\Public\\diskspace.csv&#8221; -NoTypeInformation -Force<\/p>\n<p style=\"padding-left: 30px\">#Uncomment\/comment set-alias for x86 vs. x64 system<\/p>\n<p style=\"padding-left: 30px\">#set-alias logparser &#8220;C:\\Program Files\\Log Parser 2.2\\LogParser.exe&#8221;<\/p>\n<p style=\"padding-left: 30px\">set-alias logparser &#8220;C:\\Program Files (x86)\\Log Parser 2.2\\LogParser.exe&#8221;<\/p>\n<p style=\"padding-left: 30px\">start-process -NoNewWindow -FilePath logparser -ArgumentList @&#8221;<\/p>\n<p style=\"padding-left: 30px\">&#8220;SELECT * INTO diskspaceLP FROM C:\\Users\\Public\\diskspace.csv&#8221; -i:CSV -o:SQL -server:&#8221;Win7boot\\sql1&#8243; -database:hsg -driver:&#8221;SQL Server&#8221; -createTable:ON<\/p>\n<p style=\"padding-left: 30px\">&#8220;@<\/p>\n<p>Looking at SQL Server, we see that our newly created table contains the CSV file:<\/p>\n<table border=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<p align=\"center\"><b>Filename<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Row<br \/> Number<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Usage<br \/> Date<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>System<br \/> Name<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Label<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Volume<br \/> Name<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Size<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Free<\/b><\/p>\n<\/td>\n<td>\n<p align=\"center\"><b>Percent<br \/> Free<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>C:\\Users\\Public\\diskspace.csv<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>2011-11-20<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td>\n<p>RUNCORE SSD<\/p>\n<\/td>\n<td>\n<p>D:\\<\/p>\n<\/td>\n<td>\n<p>59.62<\/p>\n<\/td>\n<td>\n<p>31.56<\/p>\n<\/td>\n<td>\n<p>52.93<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>C:\\Users\\Public\\diskspace.csv<\/p>\n<\/td>\n<td>\n<p>3<\/p>\n<\/td>\n<td>\n<p>2011-11-20<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td>\n<p>DATA<\/p>\n<\/td>\n<td>\n<p>E:\\<\/p>\n<\/td>\n<td>\n<p>297.99<\/p>\n<\/td>\n<td>\n<p>34.88<\/p>\n<\/td>\n<td>\n<p>11.7<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>C:\\Users\\Public\\diskspace.csv<\/p>\n<\/td>\n<td>\n<p>4<\/p>\n<\/td>\n<td>\n<p>2011-11-20<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td><\/td>\n<td>\n<p>C:\\<\/p>\n<\/td>\n<td>\n<p>48<\/p>\n<\/td>\n<td>\n<p>6.32<\/p>\n<\/td>\n<td>\n<p>13.16<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>C:\\Users\\Public\\diskspace.csv<\/p>\n<\/td>\n<td>\n<p>5<\/p>\n<\/td>\n<td>\n<p>2011-11-20<\/p>\n<\/td>\n<td>\n<p>WIN7BOOT<\/p>\n<\/td>\n<td>\n<p>HP_TOOLS<\/p>\n<\/td>\n<td>\n<p>F:\\<\/p>\n<\/td>\n<td>\n<p>0.1<\/p>\n<\/td>\n<td>\n<p>0.09<\/p>\n<\/td>\n<td>\n<p>96.55<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <b>CreateTable <\/b>switch will create the table if it does not exist; and if it does exist, it will simply append the rows to the existing table. Also notice that we got two new columns: Filename and Row Number, which could come in handy if we are loading a lot of CSV files. You can eliminate the Filename and Row Number columns by specifying the column list in the <b>Select<\/b> statement as we&#8217;ll see in a moment.<\/p>\n<p><strong>LogParser COM scripting<\/strong><\/p>\n<p>Using the COM-based approach to LogParser is an alternative method to using the command line. Although the COM-based approach is a little more verbose, you don&#8217;t have to worry about wrapping the execution in the <b>Start-Process<\/b> cmdlet. The COM-based approach also handles the issue with Windows Powershell ISE. Here is code to work with the COM object:<\/p>\n<p style=\"padding-left: 30px\">$logQuery = new-object -ComObject &#8220;MSUtil.LogQuery&#8221;<\/p>\n<p style=\"padding-left: 30px\">$inputFormat = new-object -comobject &#8220;MSUtil.LogQuery.CSVInputFormat&#8221;<\/p>\n<p style=\"padding-left: 30px\">$outputFormat = new-object -comobject &#8220;MSUtil.LogQuery.SQLOutputFormat&#8221;<\/p>\n<p style=\"padding-left: 30px\">$outputFormat.server = &#8220;Win7boot\\sql1&#8221;<\/p>\n<p style=\"padding-left: 30px\">$outputFormat.database = &#8220;hsg&#8221;<\/p>\n<p style=\"padding-left: 30px\">$outputFormat.driver = &#8220;SQL Server&#8221;<\/p>\n<p style=\"padding-left: 30px\">$outputFormat.createTable = $true<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;SELECT UsageDate, SystemName, Label, VolumeName, Size, Free, PercentFree INTO diskspaceLPCOM FROM C:\\Users\\Public\\diskspace.csv&#8221;<\/p>\n<p style=\"padding-left: 30px\">$null = $logQuery.ExecuteBatch($query,$inputFormat,$outputFormat)<\/p>\n<p>The main drawback to using LogParser is that it requires, well&hellip;installing LogParser. For this reason, let&#8217;s look at one more approach.<\/p>\n<h2>Use Windows PowerShell to collect server data and write to SQL Server<\/h2>\n<p>In my previous Hey, Scripting Guy! post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/11\/01\/use-powershell-to-collect-server-data-and-write-to-sql.aspx\" target=\"_blank\">Use PowerShell to Collect Server Data and Write to SQL<\/a>, I demonstrated some utility functions for loading any Windows PowerShell data into SQL Server. Let&#8217;s revisit this solution using the CSV file example:<\/p>\n<p><strong>Setup<\/strong><\/p>\n<p>Download the following scripts:<\/p>\n<ul>\n<li><a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/4208a159-a52e-4b99-83d4-8048468d29dd\/\" target=\"_blank\">Out-DataTable.ps1<\/a><\/li>\n<li><a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/c193ed1a-9152-4bda-b5c0-acd044e68b2c\/\" target=\"_blank\">Add-SqlTable.ps1<\/a><\/li>\n<li><a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/2fdeaf8d-b164-411c-9483-99413d6053ae\/\" target=\"_blank\">Write-DataTable.ps1<\/a><\/li>\n<li><a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/7985b7ef-ed89-4dfd-b02a-433cc4e30894\/\" target=\"_blank\">Invoke-SqlCmd2.ps1<\/a><\/li>\n<\/ul>\n<p>Run the following code to create a CSV file, convert to a data table, create a table in SQL Server, and load the data:<\/p>\n<p style=\"padding-left: 30px\">. .\\out-datatable.ps1<\/p>\n<p style=\"padding-left: 30px\">. .\\Add-SqlTable.ps1<\/p>\n<p style=\"padding-left: 30px\">. .\\write-datatable.ps1<\/p>\n<p style=\"padding-left: 30px\">. .\\Invoke-SqlCmd2.ps1<\/p>\n<p style=\"padding-left: 30px\">$dt = .\\Get-DiskSpaceUsage.ps1 | Out-DataTable<\/p>\n<p style=\"padding-left: 30px\">Add-SqlTable -ServerInstance &#8220;Win7boot\\Sql1&#8221; -Database &#8220;hsg&#8221; -TableName diskspaceFunc -DataTable $dt<\/p>\n<p style=\"padding-left: 30px\">Write-DataTable -ServerInstance &#8220;Win7boot\\Sql1&#8221; -Database &#8220;hsg&#8221; -TableName &#8220;diskspaceFunc&#8221; -Data $dt<\/p>\n<p style=\"padding-left: 30px\">invoke-sqlcmd2 -ServerInstance &#8220;Win7boot\\Sql1&#8221; -Database &#8220;hsg&#8221; -Query &#8220;SELECT * FROM diskspaceFunc&#8221; | Out-GridView<\/p>\n<p>The following image shows the resulting table in Grid view.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3362.hsg-11-28-11-2.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3362.hsg-11-28-11-2.jpg\" alt=\"Image of table\" title=\"Image of table\" \/><\/a><\/p>\n<p>The observant reader will notice that I didn&#8217;t write the information to a CSV file. Instead, I created an in-memory data table that is stored in my <b>$dt<\/b> variable. This is because by using this approach, there was not a need to create a CSV file, but for completeness let&#8217;s apply the solution to our CSV loading use case:<\/p>\n<p style=\"padding-left: 30px\">. .\\out-datatable.ps1<\/p>\n<p style=\"padding-left: 30px\">. .\\Add-SqlTable.ps1<\/p>\n<p style=\"padding-left: 30px\">. .\\write-datatable.ps1<\/p>\n<p style=\"padding-left: 30px\">. .\\Invoke-SqlCmd2.ps1<\/p>\n<p style=\"padding-left: 30px\">.\/get-diskspaceusage.ps1 | export-csv -Path &#8220;C:\\Users\\Public\\diskspace.csv&#8221; -NoTypeInformation -Force<\/p>\n<p style=\"padding-left: 30px\">$dt = Import-Csv -Path &#8220;C:\\Users\\Public\\diskspace.csv&#8221; | Out-DataTable<\/p>\n<p style=\"padding-left: 30px\">Add-SqlTable -ServerInstance &#8220;Win7boot\\Sql1&#8221; -Database &#8220;hsg&#8221; -TableName diskspaceFunc -DataTable $dt<\/p>\n<p style=\"padding-left: 30px\">Write-DataTable -ServerInstance &#8220;Win7boot\\Sql1&#8221; -Database &#8220;hsg&#8221; -TableName &#8220;diskspaceFunc&#8221; -Data $dt<\/p>\n<p>This post demonstrated three approaches to loading CSV files into tables in SQL Server by using a scripted approach. The approaches range from using the very simple T-SQL BULK INSERT command, to using LogParser, to using a Windows PowerShell function-based approach.<\/p>\n<p>Thank you, Chad, for sharing this information with us. It looks like your last four scripts have the makings of an awesome NetAdminCSV module.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn four easy ways to use Windows PowerShell to import CSV files into SQL Server. Microsoft Scripting Guy, Ed Wilson, is here. &nbsp;I was chatting this week with Microsoft PowerShell MVP, Chad Miller, about the series of blogs I recently wrote about using CSV files. He thought a helpful addition to the posts would [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[195,169,146,56,3,4,176,45],"class_list":["post-11951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-chad-miller","tag-csv-and-other-delimited-files","tag-databases","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-sql-server","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn four easy ways to use Windows PowerShell to import CSV files into SQL Server. Microsoft Scripting Guy, Ed Wilson, is here. &nbsp;I was chatting this week with Microsoft PowerShell MVP, Chad Miller, about the series of blogs I recently wrote about using CSV files. He thought a helpful addition to the posts would [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11951"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11951\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}