Thor Logo dbatools

New-DbaDacPackage

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

Synopsis

Creates a DACPAC package from SQL source files using the DacFx framework

Description

Creates a DACPAC (Data-tier Application Package) from SQL source files without requiring MSBuild, Visual Studio, or the .NET SDK. Uses the Microsoft.SqlServer.Dac.Model.TSqlModel API to parse SQL files, validate the model, and generate a deployable DACPAC package.

This command enables a pure PowerShell-based build workflow for database projects, making it ideal for CI/CD pipelines, development environments without Visual Studio, and cross-platform scenarios (Windows, Linux, macOS).

The DACPAC output can be deployed using Publish-DbaDacPackage, enabling idempotent schema deployments with automatic dependency ordering and drift detection.

Syntax

New-DbaDacPackage
    [-Path] <String>
    [[-OutputPath] <String>]
    [[-DacVersion] <Version>]
    [[-DacDescription] <String>]
    [[-DatabaseName] <String>]
    [-Recursive]
    [[-SqlServerVersion] <String>]
    [[-Filter] <String>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> New-DbaDacPackage -Path C:\Projects\MyDatabase\Schema -OutputPath C:\Build\MyDatabase.dacpac

Creates a DACPAC from all SQL files in C:\Projects\MyDatabase\Schema and saves it to C:\Build\MyDatabase.dacpac.

Example: 2
PS C:\> New-DbaDacPackage -Path C:\Projects\MyDatabase\Schema -Recursive -DatabaseName "MyAppDB" -DacVersion "2.1.0.0"

Creates a DACPAC from all SQL files in the Schema directory and subdirectories, setting the database name to “MyAppDB” and version to “2.1.0.0”.

Example: 3
PS C:\> New-DbaDacPackage -Path C:\Projects\MyDatabase -Recursive | Publish-DbaDacPackage -SqlInstance sql2019 -Database TestDeploy

Creates a DACPAC from source files and immediately deploys it to the TestDeploy database on sql2019.

Example: 4
PS C:\> New-DbaDacPackage -Path C:\Projects\MyDatabase\Schema -SqlServerVersion Sql140 -Recursive

Creates a DACPAC targeting SQL Server 2017 compatibility, useful when deploying to older SQL Server versions.

Example: 5
PS C:\> New-DbaDacPackage -Path C:\Projects\MyDatabase -Filter "*Table*.sql" -Recursive

Creates a DACPAC including only SQL files with “Table” in their filename.

Example: 6
PS C:\> $result = New-DbaDacPackage -Path .\sql\Schema -Recursive -DatabaseName "dbatoolspro" -DacVersion "1.0.0"
PS C:\> $result | Format-List

Creates a DACPAC and displays detailed results including object count, duration, and any errors or warnings.

Required Parameters

-Path

Specifies the directory containing SQL files to include in the DACPAC. All .sql files in the directory will be processed. Use -Recursive to include subdirectories.
Alternatively, specify a path to a .sqlproj file to parse project settings and file references from the project definition.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
Default Value

Optional Parameters

-OutputPath

Specifies the output path for the generated DACPAC file. Defaults to a file named after the DatabaseName in the current directory with .dacpac extension.
Example: If DatabaseName is “MyDatabase”, the default output would be “.\MyDatabase.dacpac”

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-DacVersion

Specifies the version number for the DACPAC package metadata. Defaults to “1.0.0.0”.
Use semantic versioning aligned with your build or release process.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value1.0.0.0
-DacDescription

Specifies an optional description to embed in the DACPAC package metadata.
Use this to document the package purpose or build context.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-DatabaseName

Specifies the database name for the DACPAC package metadata. Defaults to the name of the Path directory or current directory.
This name is used when deploying the DACPAC if no target database name is specified.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Recursive

Includes SQL files from subdirectories when Path is a directory.
Use this to process hierarchical folder structures like Schema\Tables, Schema\Views, etc.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-SqlServerVersion

Specifies the target SQL Server version for model validation and compatibility checking.
Valid values: Sql90 (2005), Sql100 (2008), Sql110 (2012), Sql120 (2014), Sql130 (2016), Sql140 (2017), Sql150 (2019), Sql160 (2022), SqlAzure.
Defaults to Sql160 (SQL Server 2022).

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueSql160
Accepted ValuesSql90,Sql100,Sql110,Sql120,Sql130,Sql140,Sql150,Sql160,SqlAzure
-Filter

Specifies a wildcard pattern to filter which SQL files to include. Defaults to “.sql”.
Use this to include only specific file patterns like “Table.sql” or “Schema_
.sql”.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value*.sql
-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 operations that change state.

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value