---
title: "Export-DbaCsv"
description: "Exports SQL Server query results or table data to CSV files with optional compression."
url: "https://dbatools.io/Export-DbaCsv/"
availability: "Windows, Linux, macOS"
tags: ["Export", "CSV", "Data", "Compression"]
author: "the dbatools team + Claude"
source: "https://github.com/dataplat/dbatools/blob/master/public/Export-DbaCsv.ps1"
last_updated: "2024-01-01"
---

# Export-DbaCsv

## 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

```powershell
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: Exports all customers from the Northwind database to a CSV file

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

### Example 2: Exports the Orders table to a GZip-compressed CSV file

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

### Example 3: Pipes table data from Get-DbaDbTable to export as CSV

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

### Example 4: Exports query results with maximum GZip compression for archival purposes

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

### Example 5: Exports to a tab-delimited file with all fields quoted

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

### Example 6: Exports query results with custom date formatting

```powershell
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"
```

### Example 7: Exports with Unicode encoding for international character support

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

### 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.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | True |
| Pipeline | false |
| Default Value |  |

### Optional Parameters

##### -SqlInstance

The target SQL Server instance or instances.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| 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.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Database

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Query

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Table

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -InputObject

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | true (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., "::", "||").

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| 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.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -Quote

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| 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.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | AsNeeded |
| Accepted Values | AsNeeded,Always,Never,NonNumeric |

##### -Encoding

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | UTF8 |
| Accepted Values | ASCII,BigEndianUnicode,Unicode,UTF7,UTF8,UTF32 |

##### -NullValue

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -DateTimeFormat

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | yyyy-MM-dd HH:mm:ss.fff |

##### -UseUtc

Converts DateTime values to UTC before formatting.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | None |
| Accepted Values | None,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.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | Optimal |
| Accepted Values | Fastest,Optimal,SmallestSize,NoCompression |

##### -Append

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -NoClobber

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

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -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.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -WhatIf

| Property | Value |
| --- | --- |
| Alias | wi |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Confirm

| Property | Value |
| --- | --- |
| Alias | cf |
| Required | False |
| Pipeline | false |
| Default Value |  |

## Outputs

**PSCustomObject**

Returns a single object containing export summary information only when rows are successfully exported. If no rows are exported, no output is returned.

**Properties:**

- Path: The full file system path where the CSV file was written (string)
- RowsExported: The total number of rows written to the CSV file (int)
- FileSizeBytes: The size of the output file in bytes (long)
- FileSizeMB: The size of the output file in megabytes, rounded to 2 decimal places (double)
- CompressionType: The compression format applied to the file - None, GZip, Deflate, Brotli, or ZLib (string)
- Elapsed: A TimeSpan object representing the total time taken to export all data (TimeSpan)
- RowsPerSecond: The average export throughput calculated as rows written per second (double)

---

Part of [dbatools](https://dbatools.io/), a free and open source PowerShell module for SQL Server administration. Full command index: https://dbatools.io/commands/
