Thor Logo dbatools

Export-DbaCsv

View Source
the dbatools team + Claude
Windows, Linux, macOS

Synopsis

Exports SQL Server query results or table data to CSV files with optional compression.

Description

Export-DbaCsv provides high-performance CSV export capabilities with support for multiple compression formats
including GZip, Deflate, Brotli, and ZLib. The function can export data from SQL queries, tables, or piped
objects to CSV files with configurable formatting options.

Supports various output formats including custom delimiters, quoting behaviors, date formatting, and encoding options.
Compression can significantly reduce file sizes for large exports, making it ideal for archiving, data transfer,
or storage-constrained environments.

Perfect for ETL processes, data exports, reporting, and creating portable data files from SQL Server.

Syntax

Export-DbaCsv
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String>]
    [[-Query] <String>]
    [[-Table] <String>]
    [[-InputObject] <Object[]>]
    [-Path] <String>
    [[-Delimiter] <String>]
    [-NoHeader]
    [[-Quote] <Char>]
    [[-QuotingBehavior] <String>]
    [[-Encoding] <String>]
    [[-NullValue] <String>]
    [[-DateTimeFormat] <String>]
    [-UseUtc]
    [[-CompressionType] <String>]
    [[-CompressionLevel] <String>]
    [-Append]
    [-NoClobber]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Export-DbaCsv -SqlInstance sql001 -Database Northwind -Query "SELECT * FROM Customers" -Path C:\temp\customers.csv

Exports all customers from the Northwind database to a CSV file.

Example: 2
PS C:\> Export-DbaCsv -SqlInstance sql001 -Database Northwind -Table "dbo.Orders" -Path C:\temp\orders.csv.gz -CompressionType GZip

Exports the Orders table to a GZip-compressed CSV file.

Example: 3
PS C:\> Get-DbaDbTable -SqlInstance sql001 -Database tempdb -Table "#MyTempTable" | Export-DbaCsv -Path C:\temp\data.csv

Pipes table data from Get-DbaDbTable to export as CSV.

Example: 4
PS C:\> Export-DbaCsv -SqlInstance sql001 -Database Sales -Query "SELECT * FROM BigTable" -Path C:\archive\data.csv.gz -CompressionType GZip -CompressionLevel SmallestSize

Exports query results with maximum GZip compression for archival purposes.

Example: 5
PS C:\> Export-DbaCsv -SqlInstance sql001 -Database HR -Table Employees -Path C:\temp\employees.csv -Delimiter "`t" -QuotingBehavior Always

Exports to a tab-delimited file with all fields quoted.

Example: 6
PS C:\> $results = Invoke-DbaQuery -SqlInstance sql001 -Database master -Query "SELECT * FROM sys.databases"
PS C:\> $results | Export-DbaCsv -Path C:\temp\databases.csv -DateTimeFormat "yyyy-MM-dd"

Exports query results with custom date formatting.

Example: 7
PS C:\> Export-DbaCsv -SqlInstance sql001 -Database Sales -Query "SELECT * FROM Orders WHERE Region = 'EMEA'" -Path C:\temp\emea.csv -Encoding Unicode

Exports with Unicode encoding for international character support.

Required Parameters

-Path

The output file path for the CSV. If the path ends with .gz, .br, .deflate, or .zlib,
the appropriate compression will be applied automatically unless -CompressionType is specified.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
Default Value

Optional Parameters

-SqlInstance

The target SQL Server instance or instances.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Database

Specifies the database to query. Required when using -Query or -Table parameters.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Query

The T-SQL query to execute. Results will be exported to CSV.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Table

The name of the table to export. Can include schema (e.g., “dbo.Customers”).

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-InputObject

Accepts piped objects to export. Can be used with results from other dbatools commands or any PowerShell objects.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-Delimiter

Sets the field separator for the CSV output. Defaults to comma.
Common values include comma (,), tab (`t), pipe (|), or semicolon (;).
Multi-character delimiters are supported (e.g., “::”, “||”).

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value,
-NoHeader

Suppresses the header row in the output. Use this when appending to existing files
or when the consuming application doesn’t expect headers.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-Quote

Specifies the character used to quote fields. Defaults to double-quote (").

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value"
-QuotingBehavior

Controls when field values are quoted.

  • AsNeeded: Quote only when necessary (contains delimiter, quote, or newline). This is the default.
  • Always: Always quote all fields.
  • Never: Never quote fields (may produce invalid CSV with some data).
  • NonNumeric: Quote only non-numeric fields.
PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueAsNeeded
Accepted ValuesAsNeeded,Always,Never,NonNumeric
-Encoding

The text encoding for the output file. Defaults to UTF8.
Valid values: ASCII, BigEndianUnicode, Unicode, UTF7, UTF8, UTF32.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueUTF8
Accepted ValuesASCII,BigEndianUnicode,Unicode,UTF7,UTF8,UTF32
-NullValue

The string to use for NULL values in the output. Defaults to empty string.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-DateTimeFormat

The format string for DateTime values. Defaults to ISO 8601 format (yyyy-MM-dd HH:mm:ss.fff).

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Valueyyyy-MM-dd HH:mm:ss.fff
-UseUtc

Converts DateTime values to UTC before formatting.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-CompressionType

The type of compression to apply to the output file.

  • None: No compression (default)
  • GZip: GZip compression (.gz)
  • Deflate: Deflate compression
  • Brotli: Brotli compression (.br) - .NET 8+ only
  • ZLib: ZLib compression - .NET 8+ only
PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueNone
Accepted ValuesNone,GZip,Deflate,Brotli,ZLib
-CompressionLevel

The compression level to use. Defaults to Optimal.

  • Fastest: Compress as fast as possible, even if the resulting file is not optimally compressed.
  • Optimal: Balance between compression speed and file size.
  • SmallestSize: Compress as much as possible, even if it takes longer.
  • NoCompression: No compression.
PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueOptimal
Accepted ValuesFastest,Optimal,SmallestSize,NoCompression
-Append

Appends to an existing file instead of overwriting. Headers are automatically suppressed when appending.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-NoClobber

Prevents overwriting an existing file. Returns an error if the file already exists.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-WhatIf
PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm
PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value