Performing bulk export using the bcp command-line utility
This recipe shows how to export contents of a table to a CSV file using PowerShell and bcp.
Getting ready
Make sure you have access to the AdventureWorks2014
database. We will export the Person.Person
table to a pipe (|
) delimited, timestamped text file.
Create a C:\Temp\Exports
folder, if you don't already have it in your system.
How to do it...
These are the steps to perform bulk export using bcp
:
Open PowerShell ISE as administrator.
Add the following script and run:
$server = "localhost" $table = "AdventureWorks2014.Person.Person" $curdate = Get-Date -Format "yyyy-MM-dd_hmmtt" $foldername = "C:\Temp\Exports\" #format file name $formatfilename = "$($table)_$($curdate).fmt" #export file name $exportfilename = "$($table)_$($curdate).csv" $destination_exportfilename = "$($foldername)$($exportfilename)" $destination_formatfilename = "$($foldername)$($formatfilename)" #command to generate format file $cmdformatfile = "bcp $table format...