dbatools is a free PowerShell module with over 300 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


Builds or extracts a SQL Server Connection String

See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
and https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx
and https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx


dbatools - New-DbaSqlConnectionString


Creates a connection string that connects using Windows Authentication.

New-DbaSqlConnectionString -SqlInstance sql2014

Builds a connected SMO object using Connect-DbaInstance then extracts and displays the connection string.

Connect-DbaInstance -SqlInstance sql2016 | New-DbaSqlConnectionString

Creates a connection string that connects using alternative Windows credentials.

$wincred = Get-Credential ad\sqladmin
New-DbaSqlConnectionString -SqlInstance sql2014 -Credential $wincred

Login to sql2014 as SQL login sqladmin.

$sqlcred = Get-Credential sqladmin
$server = New-DbaSqlConnectionString -SqlInstance sql2014 -Credential $sqlcred

Creates a connection string that connects using Windows Authentication and uses the client name “mah connection”. So when you open up profiler or use extended events, you can search for “mah connection”.

$server = New-DbaSqlConnectionString -SqlInstance sql2014 -ClientName “mah connection”

Creates a connection string with ReadOnly ApplicantionIntent.

$connstring = New-DbaSqlConnectionString sql2016 -ApplicationIntent ReadOnly


This command was created by Chrissy LeMaire. You can find Chrissy on Twitter.


From PowerShell, execute Get-Help New-DbaSqlConnectionString -Detailed for more information on this function.

Source Code

Want to see the source code? View New-DbaSqlConnectionString.ps1 on GitHub

Related commands