< Back
New-DbaConnectionString
Post
NAME New-DbaConnectionString
SYNOPSIS
Builds or extracts a SQL Server Connection String
SYNTAX
New-DbaConnectionString [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-Credential]
<Pscredential>] [[-AccessToken] <String>] [[-ApplicationIntent] <String>] [[-BatchSeparator] <String>]
[[-ClientName] <String>] [[-ConnectTimeout] <Int>] [[-Database] <String>] [-EncryptConnection <Switch>]
[[-FailoverPartner] <String>] [-IsActiveDirectoryUniversalAuth <Switch>] [[-LockTimeout] <Int>] [[-MaxPoolSize]
<Int>] [[-MinPoolSize] <Int>] [-MultipleActiveResultSets <Switch>] [-MultiSubnetFailover <Switch>]
[[-NetworkProtocol] <String>] [-NonPooledConnection <Switch>] [[-PacketSize] <Int>] [[-PooledConnectionLifetime]
<Int>] [[-SqlExecutionModes] <String>] [[-StatementTimeout] <Int>] [-TrustServerCertificate <Switch>]
[[-WorkstationId] <String>] [[-AppendConnectionString] <String>] [<CommonParameters>]
DESCRIPTION
Builds or extracts a SQL Server Connection String. Note that dbatools-style syntax is used.
So you do not need to specify "Data Source", you can just specify -SqlInstance and -SqlCredential and we'll handle
it for you.
This is the simplified PowerShell approach to connection string building. See examples for more info.
See https://msdn.microsoft.com/en-us/librar ... tring.aspx
and https://msdn.microsoft.com/en-us/librar ... ilder.aspx
and https://msdn.microsoft.com/en-us/librar ... ction.aspx
PARAMETERS
-AccessToken [<String>]
Basically tells the connection string to ignore authentication. Does not include the AccessToken in the
resulting connecstring.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-AppendConnectionString [<String>]
Appends to the current connection string. Note that you cannot pass authentication information using this
method. Use -SqlInstance and, optionally, -SqlCredential to set authentication information.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ApplicationIntent [<String>]
Declares the application workload type when connecting to a server. Possible values are ReadOnly and ReadWrite.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSeparator [<String>]
By default, this is "GO"
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ClientName [<String>]
By default, this command sets the client to "dbatools PowerShell module - dbatools.io - custom connection" if
you're doing anything that requires profiling, you can look for this client name. Using -ClientName allows you
to set your own custom client.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ConnectTimeout [<Int>]
The length of time (in seconds) to wait for a connection to the server before terminating the attempt and
generating an error.
Valid values are greater than or equal to 0 and less than or equal to 2147483647.
When opening a connection to a Azure SQL Database, set the connection timeout to 30 seconds.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Credential [<Pscredential>]
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. be it Windows or SQL Server. Windows users are determined by
the existence of a backslash, so if you are intending to use an alternative Windows connection instead of a
SQL login, ensure it contains a backslash.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<String>]
Database name
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EncryptConnection [<Switch>]
When true, SQL Server uses SSL encryption for all data sent between the client and server if the server has a
certificate installed. Recognized values are true, false, yes, and no. For more information, see Connection
String Syntax.
Beginning in .NET Framework 4.5, when TrustServerCertificate is false and Encrypt is true, the server name (or
IP address) in a SQL Server SSL certificate must exactly match the server name (or IP address) specified in
the connection string. Otherwise, the connection attempt will fail. For information about support for
certificates whose subject starts with a wildcard character (*), see Accepted wildcards used by server
certificates for server authentication.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FailoverPartner [<String>]
The name of the failover partner server where database mirroring is configured.
If the value of this key is "", then Initial Catalog must be present, and its value must not be "".
The server name can be 128 characters or less.
If you specify a failover partner but the failover partner server is not configured for database mirroring and
the primary server (specified with the Server keyword) is not available, then the connection will fail.
If you specify a failover partner and the primary server is not configured for database mirroring, the
connection to the primary server (specified with the Server keyword) will succeed if the primary server is
available.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-IsActiveDirectoryUniversalAuth [<Switch>]
Azure related
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-LockTimeout [<Int>]
Sets the time in seconds required for the connection to time out when the current transaction is locked.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MaxPoolSize [<Int>]
Sets the maximum number of connections allowed in the connection pool for this specific connection string.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MinPoolSize [<Int>]
Sets the minimum number of connections allowed in the connection pool for this specific connection string.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MultipleActiveResultSets [<Switch>]
When used, an application can maintain multiple active result sets (MARS). When false, an application must
process or cancel all result sets from one batch before it can execute any other batch on that connection.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MultiSubnetFailover [<Switch>]
If your application is connecting to an AlwaysOn availability group (AG) on different subnets, setting
MultiSubnetFailover provides faster detection of and connection to the (currently) active server. For more
information about SqlClient support for Always On Availability Groups
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NetworkProtocol [<String>]
Connect explicitly using 'TcpIp','NamedPipes','Multiprotocol','AppleTalk','BanyanVines','Via','SharedMemory'
and 'NWLinkIpxSpx'
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NonPooledConnection [<Switch>]
Request a non-pooled connection
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PacketSize [<Int>]
Sets the size in bytes of the network packets used to communicate with an instance of SQL Server. Must match
at server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PooledConnectionLifetime [<Int>]
When a connection is returned to the pool, its creation time is compared with the current time, and the
connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime.
This is useful in clustered configurations to force load balancing between a running server and a server just
brought online.
A value of zero (0) causes pooled connections to have the maximum connection timeout.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlExecutionModes [<String>]
The SqlExecutionModes enumeration contains values that are used to specify whether the commands sent to the
referenced connection to the server are executed immediately or saved in a buffer.
Valid values include CaptureSql, ExecuteAndCaptureSql and ExecuteSql.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StatementTimeout [<Int>]
Sets the number of seconds a statement is given to run before failing with a time-out error.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-TrustServerCertificate [<Switch>]
Sets a value that indicates whether the channel will be encrypted while bypassing walking the certificate
chain to validate trust.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-WorkstationId [<String>]
Sets the name of the workstation connecting to SQL Server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Tags: Connection, Connect, ConnectionString
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-DbaConnectionString -SqlInstance sql2014
Creates a connection string that connects using Windows Authentication
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Connect-DbaInstance -SqlInstance sql2016 | New-DbaConnectionString
Builds a connected SMO object using Connect-DbaInstance then extracts and displays the connection string
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>$wincred = Get-Credential ad\\sqladmin
PS C:\\> New-DbaConnectionString -SqlInstance sql2014 -Credential $wincred
Creates a connection string that connects using alternative Windows credentials
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>$sqlcred = Get-Credential sqladmin
PS C:\\> $server = New-DbaConnectionString -SqlInstance sql2014 -Credential $sqlcred
Login to sql2014 as SQL login sqladmin.
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>$connstring = New-DbaConnectionString -SqlInstance mydb.database.windows.net -SqlCredential
me@myad.onmicrosoft.com -Database db
Creates a connection string for an Azure Active Directory login to Azure SQL db. Output looks like this:
Data Source=TCP:mydb.database.windows.net,1433;Initial Catalog=db;User
ID=me@myad.onmicrosoft.com;Password=fakepass;MultipleActiveResultSets=False;Connect
Timeout=30;Encrypt=True;TrustServerCertificate=False;Application Name="dbatools PowerShell module -
dbatools.io";Authentication="Active Directory Password"
-------------------------- EXAMPLE 6 --------------------------
PS C:\\>$server = New-DbaConnectionString -SqlInstance sql2014 -ClientName "mah connection"
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".
-------------------------- EXAMPLE 7 --------------------------
PS C:\\>$server = New-DbaConnectionString -SqlInstance sql2014 -AppendConnectionString "Packet
Size=4096;AttachDbFilename=C:\\MyFolder\\MyDataFile.mdf;User Instance=true;"
Creates a connection string that connects to sql2014 using Windows Authentication, then it sets the packet size
(this can also be done via -PacketSize) and other connection attributes.
-------------------------- EXAMPLE 8 --------------------------
PS C:\\>$server = New-DbaConnectionString -SqlInstance sql2014 -NetworkProtocol TcpIp -MultiSubnetFailover
Creates a connection string with Windows Authentication that uses TCPIP and has MultiSubnetFailover enabled.
-------------------------- EXAMPLE 9 --------------------------
PS C:\\>$connstring = New-DbaConnectionString sql2016 -ApplicationIntent ReadOnly
Creates a connection string with ReadOnly ApplicationIntent.
RELATED LINKS
https://dbatools.io/New-DbaConnectionString
SYNOPSIS
Builds or extracts a SQL Server Connection String
SYNTAX
New-DbaConnectionString [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-Credential]
<Pscredential>] [[-AccessToken] <String>] [[-ApplicationIntent] <String>] [[-BatchSeparator] <String>]
[[-ClientName] <String>] [[-ConnectTimeout] <Int>] [[-Database] <String>] [-EncryptConnection <Switch>]
[[-FailoverPartner] <String>] [-IsActiveDirectoryUniversalAuth <Switch>] [[-LockTimeout] <Int>] [[-MaxPoolSize]
<Int>] [[-MinPoolSize] <Int>] [-MultipleActiveResultSets <Switch>] [-MultiSubnetFailover <Switch>]
[[-NetworkProtocol] <String>] [-NonPooledConnection <Switch>] [[-PacketSize] <Int>] [[-PooledConnectionLifetime]
<Int>] [[-SqlExecutionModes] <String>] [[-StatementTimeout] <Int>] [-TrustServerCertificate <Switch>]
[[-WorkstationId] <String>] [[-AppendConnectionString] <String>] [<CommonParameters>]
DESCRIPTION
Builds or extracts a SQL Server Connection String. Note that dbatools-style syntax is used.
So you do not need to specify "Data Source", you can just specify -SqlInstance and -SqlCredential and we'll handle
it for you.
This is the simplified PowerShell approach to connection string building. See examples for more info.
See https://msdn.microsoft.com/en-us/librar ... tring.aspx
and https://msdn.microsoft.com/en-us/librar ... ilder.aspx
and https://msdn.microsoft.com/en-us/librar ... ction.aspx
PARAMETERS
-AccessToken [<String>]
Basically tells the connection string to ignore authentication. Does not include the AccessToken in the
resulting connecstring.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-AppendConnectionString [<String>]
Appends to the current connection string. Note that you cannot pass authentication information using this
method. Use -SqlInstance and, optionally, -SqlCredential to set authentication information.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ApplicationIntent [<String>]
Declares the application workload type when connecting to a server. Possible values are ReadOnly and ReadWrite.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSeparator [<String>]
By default, this is "GO"
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ClientName [<String>]
By default, this command sets the client to "dbatools PowerShell module - dbatools.io - custom connection" if
you're doing anything that requires profiling, you can look for this client name. Using -ClientName allows you
to set your own custom client.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ConnectTimeout [<Int>]
The length of time (in seconds) to wait for a connection to the server before terminating the attempt and
generating an error.
Valid values are greater than or equal to 0 and less than or equal to 2147483647.
When opening a connection to a Azure SQL Database, set the connection timeout to 30 seconds.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Credential [<Pscredential>]
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. be it Windows or SQL Server. Windows users are determined by
the existence of a backslash, so if you are intending to use an alternative Windows connection instead of a
SQL login, ensure it contains a backslash.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<String>]
Database name
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EncryptConnection [<Switch>]
When true, SQL Server uses SSL encryption for all data sent between the client and server if the server has a
certificate installed. Recognized values are true, false, yes, and no. For more information, see Connection
String Syntax.
Beginning in .NET Framework 4.5, when TrustServerCertificate is false and Encrypt is true, the server name (or
IP address) in a SQL Server SSL certificate must exactly match the server name (or IP address) specified in
the connection string. Otherwise, the connection attempt will fail. For information about support for
certificates whose subject starts with a wildcard character (*), see Accepted wildcards used by server
certificates for server authentication.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FailoverPartner [<String>]
The name of the failover partner server where database mirroring is configured.
If the value of this key is "", then Initial Catalog must be present, and its value must not be "".
The server name can be 128 characters or less.
If you specify a failover partner but the failover partner server is not configured for database mirroring and
the primary server (specified with the Server keyword) is not available, then the connection will fail.
If you specify a failover partner and the primary server is not configured for database mirroring, the
connection to the primary server (specified with the Server keyword) will succeed if the primary server is
available.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-IsActiveDirectoryUniversalAuth [<Switch>]
Azure related
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-LockTimeout [<Int>]
Sets the time in seconds required for the connection to time out when the current transaction is locked.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MaxPoolSize [<Int>]
Sets the maximum number of connections allowed in the connection pool for this specific connection string.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MinPoolSize [<Int>]
Sets the minimum number of connections allowed in the connection pool for this specific connection string.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MultipleActiveResultSets [<Switch>]
When used, an application can maintain multiple active result sets (MARS). When false, an application must
process or cancel all result sets from one batch before it can execute any other batch on that connection.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-MultiSubnetFailover [<Switch>]
If your application is connecting to an AlwaysOn availability group (AG) on different subnets, setting
MultiSubnetFailover provides faster detection of and connection to the (currently) active server. For more
information about SqlClient support for Always On Availability Groups
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NetworkProtocol [<String>]
Connect explicitly using 'TcpIp','NamedPipes','Multiprotocol','AppleTalk','BanyanVines','Via','SharedMemory'
and 'NWLinkIpxSpx'
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NonPooledConnection [<Switch>]
Request a non-pooled connection
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PacketSize [<Int>]
Sets the size in bytes of the network packets used to communicate with an instance of SQL Server. Must match
at server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PooledConnectionLifetime [<Int>]
When a connection is returned to the pool, its creation time is compared with the current time, and the
connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime.
This is useful in clustered configurations to force load balancing between a running server and a server just
brought online.
A value of zero (0) causes pooled connections to have the maximum connection timeout.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlExecutionModes [<String>]
The SqlExecutionModes enumeration contains values that are used to specify whether the commands sent to the
referenced connection to the server are executed immediately or saved in a buffer.
Valid values include CaptureSql, ExecuteAndCaptureSql and ExecuteSql.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StatementTimeout [<Int>]
Sets the number of seconds a statement is given to run before failing with a time-out error.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-TrustServerCertificate [<Switch>]
Sets a value that indicates whether the channel will be encrypted while bypassing walking the certificate
chain to validate trust.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-WorkstationId [<String>]
Sets the name of the workstation connecting to SQL Server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Tags: Connection, Connect, ConnectionString
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-DbaConnectionString -SqlInstance sql2014
Creates a connection string that connects using Windows Authentication
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Connect-DbaInstance -SqlInstance sql2016 | New-DbaConnectionString
Builds a connected SMO object using Connect-DbaInstance then extracts and displays the connection string
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>$wincred = Get-Credential ad\\sqladmin
PS C:\\> New-DbaConnectionString -SqlInstance sql2014 -Credential $wincred
Creates a connection string that connects using alternative Windows credentials
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>$sqlcred = Get-Credential sqladmin
PS C:\\> $server = New-DbaConnectionString -SqlInstance sql2014 -Credential $sqlcred
Login to sql2014 as SQL login sqladmin.
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>$connstring = New-DbaConnectionString -SqlInstance mydb.database.windows.net -SqlCredential
me@myad.onmicrosoft.com -Database db
Creates a connection string for an Azure Active Directory login to Azure SQL db. Output looks like this:
Data Source=TCP:mydb.database.windows.net,1433;Initial Catalog=db;User
ID=me@myad.onmicrosoft.com;Password=fakepass;MultipleActiveResultSets=False;Connect
Timeout=30;Encrypt=True;TrustServerCertificate=False;Application Name="dbatools PowerShell module -
dbatools.io";Authentication="Active Directory Password"
-------------------------- EXAMPLE 6 --------------------------
PS C:\\>$server = New-DbaConnectionString -SqlInstance sql2014 -ClientName "mah connection"
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".
-------------------------- EXAMPLE 7 --------------------------
PS C:\\>$server = New-DbaConnectionString -SqlInstance sql2014 -AppendConnectionString "Packet
Size=4096;AttachDbFilename=C:\\MyFolder\\MyDataFile.mdf;User Instance=true;"
Creates a connection string that connects to sql2014 using Windows Authentication, then it sets the packet size
(this can also be done via -PacketSize) and other connection attributes.
-------------------------- EXAMPLE 8 --------------------------
PS C:\\>$server = New-DbaConnectionString -SqlInstance sql2014 -NetworkProtocol TcpIp -MultiSubnetFailover
Creates a connection string with Windows Authentication that uses TCPIP and has MultiSubnetFailover enabled.
-------------------------- EXAMPLE 9 --------------------------
PS C:\\>$connstring = New-DbaConnectionString sql2016 -ApplicationIntent ReadOnly
Creates a connection string with ReadOnly ApplicationIntent.
RELATED LINKS
https://dbatools.io/New-DbaConnectionString