Thor Logo dbatools

Restore-DbaDbCertificate

View Source
Jess Pomfret (@jpomfret), jesspomfret.com
Windows, Linux, macOS

Synopsis

Restores database certificates from .cer and .pvk files into SQL Server databases.

Description

Restores database certificates and their associated private keys from backup files into SQL Server databases. This function is essential for recovering certificates used in TDE (Transparent Data Encryption), backup encryption, Always Encrypted, and other SQL Server security features after database migrations, disaster recovery, or server rebuilds.

The function automatically locates matching private key files (.pvk) for each certificate (.cer) when processing directories, or you can specify key file paths explicitly. Handles password-protected private keys with secure credential management, and allows you to re-encrypt keys during the restore process if needed.

Syntax

Restore-DbaDbCertificate
    [-SqlInstance] <DbaInstanceParameter>
    [[-SqlCredential] <PSCredential>]
    [-Path] <String[]>
    [[-KeyFilePath] <String[]>]
    [[-EncryptionPassword] <SecureString>]
    [[-Database] <String>]
    [[-Name] <String>]
    [[-DecryptionPassword] <SecureString>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> $securepass = Get-Credential usernamedoesntmatter | Select-Object -ExpandProperty Password
PS C:\> Restore-DbaDbCertificate -SqlInstance Server1 -Path \\Server1\Certificates -DecryptionPassword $securepass

Restores all the certificates in the specified path, password is used to both decrypt and encrypt the private key.

Example: 2
PS C:\> Restore-DbaDbCertificate -SqlInstance Server1 -Path \\Server1\Certificates\DatabaseTDE.cer -DecryptionPassword (Get-Credential usernamedoesntmatter).Password

Restores the DatabaseTDE certificate to Server1 and uses the MasterKey to encrypt the private key.

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
Default Value
-Path

Specifies the file system path to certificate files (.cer) or a directory containing multiple certificates. When pointing to a directory, the function processes all .cer files found within it.
Use this to restore certificates from your certificate backup location after disaster recovery or server migrations.

PropertyValue
AliasFullName,ExportPath
RequiredTrue
Pipelinetrue (ByPropertyName)
Default Value

Optional Parameters

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

Specifies the path to the private key file (.pvk) associated with the certificate. If not provided, the function automatically searches for a matching .pvk file in the same directory as the
certificate.
Only specify this when your private key files are stored in a different location from your certificate files.

PropertyValue
AliasKey
RequiredFalse
Pipelinetrue (ByPropertyName)
Default Value
-EncryptionPassword

Sets a new password to encrypt the private key after restoration to SQL Server. If not specified, the restored certificate will be encrypted with the database master key.
Use this when you want to change the private key encryption method or set a specific password for the restored certificate.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Database

Specifies the target database where the certificate will be restored. Defaults to the master database if not specified.
Use this when restoring certificates for specific database features like TDE, Always Encrypted, or application-specific encryption within user databases.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Valuemaster
-Name

Specifies a custom name for the restored certificate in SQL Server. If not provided, the function derives the name from the certificate file name, removing instance and database prefixes.
Use this when you need the certificate to have a specific name that differs from the backup file naming convention.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-DecryptionPassword

Provides the password required to decrypt the private key file (.pvk) during certificate restoration. This password was set when the certificate was originally backed up.
Required for all certificate restores since private keys are encrypted by default when exported from SQL Server.

PropertyValue
AliasPassword,SecurePassword
RequiredFalse
Pipelinefalse
Default Value(Read-Host “Decryption password” -AsSecureString)
-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

Shows what would happen if the command were to run. No actions are actually performed.

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value