<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://turecki.net"  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>Michał Turecki - csv</title>
 <link>https://turecki.net/tags/csv</link>
 <description></description>
 <language>en</language>
<item>
 <title>How to save data from all tables in SQL Server database into CSV files (using PowerShell)</title>
 <link>https://turecki.net/content/how-save-data-all-tables-sql-server-database-csv-files-using-powershell</link>
 <description>&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;All it takes is to create the following PowerShell script (text file with .ps1 extension) and changing $server and $db variables to match the server&#039;s host and instance name (SERVER\INSTANCE) and a database name.&lt;/p&gt;
&lt;pre&gt;
$destDir = Get-Location
$server = &quot;localhost&quot;
$db = &quot;DatabaseToExport&quot;

Import-Module -Name SQLPS
$tables = Invoke-Sqlcmd -query &quot;SELECT name FROM sys.tables&quot; -Database &quot;$db&quot; -ServerInstance &quot;$server&quot;
foreach ($table in $tables)
{
	$tableName = $table[&quot;name&quot;]
	write-host -ForegroundColor Green &quot;Creating File $tableName.csv&quot;
	Invoke-Sqlcmd -Query &quot;SELECT * FROM $tableName;&quot; -Database &quot;$db&quot; -Server &quot;$server&quot; |
		Export-Csv -NoTypeInformation -Path &quot;$destDir\$tableName.csv&quot; -Encoding UTF8
}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;a href=&quot;/tags/powershell&quot;&gt;powershell&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;field-item odd&quot;&gt;&lt;a href=&quot;/tags/sql-server&quot;&gt;sql-server&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;a href=&quot;/tags/database&quot;&gt;database&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;field-item odd&quot;&gt;&lt;a href=&quot;/tags/csv&quot;&gt;csv&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Wed, 23 Sep 2015 09:24:44 +0000</pubDate>
 <dc:creator>Michał Turecki</dc:creator>
 <guid isPermaLink="false">28 at https://turecki.net</guid>
 <comments>https://turecki.net/content/how-save-data-all-tables-sql-server-database-csv-files-using-powershell#comments</comments>
</item>
</channel>
</rss>
