New-DbaDacPackage
View SourceSynopsis
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.
| Property | Value |
|---|---|
| Alias | |
| Required | True |
| Pipeline | false |
| 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”
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| 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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | 1.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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| 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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| 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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
-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).
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | Sql160 |
| Accepted Values | Sql90,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”.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| 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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
-WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
| Property | Value |
|---|---|
| Alias | wi |
| Required | False |
| Pipeline | false |
| Default Value |
-Confirm
Prompts you for confirmation before executing any operations that change state.
| Property | Value |
|---|---|
| Alias | cf |
| Required | False |
| Pipeline | false |
| Default Value |
Outputs
PSCustomObject
Returns one result object per DACPAC build operation.
On successful build:
- ComputerName: The local computer name where the DACPAC was built
- Path: Full file path to the created DACPAC file
- Database: The database name embedded in the DACPAC package metadata
- DatabaseName: Database name (same as Database property)
- Version: Version string in semantic version format (e.g., “1.0.0.0”)
- FileCount: Number of SQL files processed into the DACPAC
- ObjectCount: Number of database objects in the compiled model
- Duration: Elapsed time for the build operation (prettytimespan object, displays as human-readable duration)
- Success: Boolean True indicating successful DACPAC creation
- Errors: Array of error messages encountered (empty array on success)
- Warnings: Array of warning messages from validation (empty array if no warnings)
Default display properties (via Select-DefaultView):
- Path: File path to the DACPAC
- DatabaseName: Database name
- Version: Package version
- FileCount: SQL files processed
- ObjectCount: Database objects compiled
- Duration: Build time elapsed
- Success: Build status
On build failure (when validation errors prevent DACPAC creation):
- DacpacPath: Null (DACPAC was not created)
- DatabaseName: Database name
- Version: Version string
- FileCount: Number of files attempted
- ObjectCount: Object count at time of failure
- Duration: Elapsed time before failure
- Success: Boolean False
- Errors: Array of validation and processing errors preventing build
- Warnings: Array of warnings encountered before failure All objects are returned as PSCustomObject with properties accessible via dot notation or Select-Object. The output is pipeline-compatible with Publish-DbaDacPackage for automated deployment workflows. PSCustomObject Returns one result object per DACPAC build operation.
On successful build:
- ComputerName: The local computer name where the DACPAC was built
- Path: Full file path to the created DACPAC file
- Database: The database name embedded in the DACPAC package metadata
- DatabaseName: Database name (same as Database property)
- Version: Version string in semantic version format (e.g., “1.0.0.0”)
- FileCount: Number of SQL files processed into the DACPAC
- ObjectCount: Number of database objects in the compiled model
- Duration: Elapsed time for the build operation (prettytimespan object, displays as human-readable duration)
- Success: Boolean True indicating successful DACPAC creation
- Errors: Array of error messages encountered (empty array on success)
- Warnings: Array of warning messages from validation (empty array if no warnings)
Default display properties (via Select-DefaultView):
- Path: File path to the DACPAC
- DatabaseName: Database name
- Version: Package version
- FileCount: SQL files processed
- ObjectCount: Database objects compiled
- Duration: Build time elapsed
- Success: Build status
On build failure (when validation errors prevent DACPAC creation):
- DacpacPath: Null (DACPAC was not created)
- DatabaseName: Database name
- Version: Version string
- FileCount: Number of files attempted
- ObjectCount: Object count at time of failure
- Duration: Elapsed time before failure
- Success: Boolean False
- Errors: Array of validation and processing errors preventing build
- Warnings: Array of warnings encountered before failure All objects are returned as PSCustomObject with properties accessible via dot notation or Select-Object. The output is pipeline-compatible with Publish-DbaDacPackage for automated deployment workflows.
dbatools