Skip to content

Instantly share code, notes, and snippets.

@paschott
Last active February 27, 2025 23:12
Show Gist options
  • Save paschott/62b2f7c2ca84513fa908696581291c19 to your computer and use it in GitHub Desktop.
Save paschott/62b2f7c2ca84513fa908696581291c19 to your computer and use it in GitHub Desktop.
Use PowerShell, the sqlserver module, and BCP.exe to extract data into files
Import-Module sqlserver
$serverName = "localhost"
$ExportFolder = "C:\temp\$serverName"
$databaseName = "AdventureWorks"
$tablesToExport = 'Person.Address','Person.AddressType','Person.BusinessEntity','Person.BusinessEntityAddress','Person.BusinessEntityContact','Person.ContactType','Person.CountryRegion','Person.EmailAddress','Person.Password','Person.Person','Person.PersonPhone','Person.PhoneNumberType','Person.StateProvince'
New-item -ItemType Directory -Path $ExportFolder -Force
foreach ($table in $tablesToExport) {
Write-Host "Exporting $table to file...."
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Schema + '.' + Table_Name = '$table' ORDER BY ORDINAL_POSITION"
<# CSV Export #>
# <# Comment this line out for CSV
$TempOutputFile = Join-Path -Path $ExportFolder -ChildPath "$table_temp.csv"
$OutputFile = Join-Path -Path $ExportFolder -ChildPath "$table.csv"
$delimiter = ","
$bcpCommand = "bcp $table out '$TempOutputFile' -d $databaseName -c -t ',' -S $serverName -T -TrustServerCertificate -r '`r`n' -C 65001"
Invoke-Expression $bcpCommand
$headers = (Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query $query -TrustServerCertificate |
Select-Object -ExpandProperty COLUMN_NAME) -join $delimiter
Set-Content -Path $OutputFile -Value $headers -Force #write header data
Get-Content $TempOutputFile | Add-Content -Path $OutputFile #append data
Remove-Item $TempOutputFile -Force
Write-Output "Exported $table to $OutputFile"
<# End CSV Export#>
<#####################################################################################>
<# Tab-delimited Export #>
# <# Comment this line out for Tab-delimited
$TempOutputFile = Join-Path -Path $ExportFolder -ChildPath "$table_temp.tab"
$OutputFile = Join-Path -Path $ExportFolder -ChildPath "$table.tab"
$delimiter = "`t"
$bcpCommand = "bcp '$table' out '$TempOutputFile' -d $databaseName -c -S $serverName -T -TrustServerCertificate -r '`r`n' -C 65001"
Invoke-Expression $bcpCommand
$headers = (Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query $query -TrustServerCertificate |
Select-Object -ExpandProperty COLUMN_NAME) -join $delimiter
Set-Content -Path $OutputFile -Value $headers -Force #write header data
Get-Content $TempOutputFile | Add-Content -Path $OutputFile #append data
Remove-Item $TempOutputFile -Force
Write-Output "Exported $table to $OutputFile"
<# End Tab-delimited Export#>
<#####################################################################################>
<# Pipe-delimited Export #>
# <# Comment this line out for Pipe-delimited
$TempOutputFile = Join-Path -Path $ExportFolder -ChildPath "$table_temp.dat"
$OutputFile = Join-Path -Path $ExportFolder -ChildPath "$table.dat"
$delimiter = "|"
$bcpCommand = "bcp '$table' out '$TempOutputFile' -d $databaseName -c -t '|' -S $serverName -T -TrustServerCertificate -r '`r`n' -C 65001"
Invoke-Expression $bcpCommand
$headers = (Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query $query -TrustServerCertificate |
Select-Object -ExpandProperty COLUMN_NAME) -join $delimiter
Set-Content -Path $OutputFile -Value $headers -Force #write header data
Get-Content $TempOutputFile | Add-Content -Path $OutputFile #append data
Remove-Item $TempOutputFile -Force
Write-Output "Exported $table to $OutputFile"
<# End Pipe-delimited Export#>
<#####################################################################################>
<# BCP Native Export #>
# <# Comment this line out for BCP Native
$OutputFile = Join-Path -Path $ExportFolder -ChildPath "$table.bcp"
$bcpCommand = "bcp $table out '$OutputFile' -d $databaseName -n -S $serverName -T -TrustServerCertificate"
Invoke-Expression $bcpCommand
Write-Output "Exported $table to $OutputFile"
<# End BCP Native Export#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment