---
title: "Export-DbaCredential"
description: "Exports SQL Server credentials to executable T-SQL CREATE CREDENTIAL scripts"
url: "https://dbatools.io/Export-DbaCredential/"
availability: "Windows only"
tags: ["Credential"]
author: "Chrissy LeMaire (@cl), netnerds.net"
source: "https://github.com/dataplat/dbatools/blob/master/public/Export-DbaCredential.ps1"
last_updated: "2024-01-01"
---

# Export-DbaCredential

## Synopsis

Exports SQL Server credentials to executable T-SQL CREATE CREDENTIAL scripts

## Description

Exports SQL Server credentials to T-SQL files containing CREATE CREDENTIAL statements that can recreate the credentials on another instance. By default, this includes decrypted passwords, making it perfect for migration scenarios where you need to move credentials between servers.  
  
The function generates executable T-SQL scripts that DBAs can run to recreate credentials during migrations, disaster recovery, or when setting up new environments. When passwords are included, the function requires sysadmin privileges and remote Windows registry access to decrypt the stored secrets.  
  
Use the ExcludePassword parameter to export credential definitions without sensitive data for documentation or security-conscious scenarios.

## Syntax

```powershell
Export-DbaCredential
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Credential] <PSCredential>]
    [[-Path] <String>]
    [[-FilePath] <String>]
    [[-Identity] <String[]>]
    [-ExcludePassword]
    [-Append]
    [-Passthru]
    [-EnableException]
    [<CommonParameters>]

```

## Examples

### Example 1: Exports credentials, including passwords, from sql2017 to the file C:\temp\cred.sql

```powershell
PS C:\> Export-DbaCredential -SqlInstance sql2017 -Path C:\temp\cred.sql
```

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

##### -Credential

Login to the target OS using alternative credentials. Accepts credential objects (Get-Credential)  
Only used when passwords are being exported, as it requires access to the Windows OS via PowerShell remoting to decrypt the passwords.

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

##### -Path

Specifies the directory where the exported T-SQL script file will be saved. Defaults to the configured DbatoolsExport path.  
Use this when you want to control where credential scripts are stored for organization or compliance requirements.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport') |

##### -FilePath

Specifies the complete file path and name for the exported T-SQL script. Overrides the Path parameter when specified.  
Use this when you need precise control over the output file name and location, especially for automated processes.

| Property | Value |
| --- | --- |
| Alias | OutFile,FileName |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Identity

Specifies which credential names to export by filtering on the Identity property. Accepts an array of credential names.  
Use this to export specific credentials instead of all credentials, particularly useful when migrating only certain application or service accounts.

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

##### -ExcludePassword

Exports credential definitions without the actual password values, replacing them with placeholder text.  
Use this for documentation purposes or when you need credential structure without sensitive data for security reviews.

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

##### -Append

Adds the exported credential scripts to an existing file instead of overwriting it.  
Use this when consolidating credentials from multiple instances into a single deployment script.

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

##### -Passthru

Returns the generated T-SQL script to the PowerShell pipeline instead of saving to file.  
Use this to capture the script in a variable, pipe to other commands, or display directly in the console.

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

## Outputs

**System.IO.FileInfo**

Returns a file object representing the exported T-SQL script file(s) containing the CREATE CREDENTIAL statements. One file is returned for each SQL Server instance from which credentials were exported.

**Properties:**

- FullName: The complete path to the exported script file
- Name: The name of the exported script file
- Length: The size of the exported file in bytes
- LastWriteTime: The date and time the file was created or last modified
- Directory: The directory containing the exported file

---

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