< Back
Export-DbaLogin
Post
NAME Export-DbaLogin
SYNOPSIS
Exports Windows and SQL Logins to a T-SQL file. Export includes login, SID, password, default database, default
language, server permissions, server roles, db permissions, db roles.
SYNTAX
Export-DbaLogin [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]
<Pscredential>] [[-InputObject] <System.Object[]>] [[-Login] <System.Object[]>] [[-ExcludeLogin]
<System.Object[]>] [[-Database] <System.Object[]>] [-ExcludeJobs <Switch>] [-ExcludeDatabase <Switch>]
[-ExcludePassword <Switch>] [[-DefaultDatabase] <String>] [[-Path] <String>] [[-FilePath] <String>] [[-Encoding]
<String>] [-NoClobber <Switch>] [-Append <Switch>] [[-BatchSeparator] <String>] [[-DestinationVersion] <String>]
[-NoPrefix <Switch>] [-Passthru <Switch>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Exports Windows and SQL Logins to a T-SQL file. Export includes login, SID, password, default database, default
language, server permissions, server roles, db permissions, db roles.
PARAMETERS
-Append [<Switch>]
If this switch is enabled, content will be appended to a file already existing at the path specified by Path.
If the file does not exist, it will be created.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSeparator [<String>]
Batch separator for scripting output. Uses the value from configuration Formatting.BatchSeparator by default.
This is normally "GO"
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all
databases will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DefaultDatabase [<String>]
If this switch is enabled, all logins will be scripted with specified default database,
that could help to successfully import logins on server that is missing default database for login.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationVersion [<String>]
To say to which version the script should be generated. If not specified will use instance major version.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Encoding [<String>]
Specifies the file encoding. The default is UTF8.
Valid values are:
-- ASCII: Uses the encoding for the ASCII (7-bit) character set.
-- BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order.
-- Byte: Encodes a set of characters into a sequence of bytes.
-- String: Uses the encoding type for a string.
-- Unicode: Encodes in UTF-16 format using the little-endian byte order.
-- UTF7: Encodes in UTF-7 format.
-- UTF8: Encodes in UTF-8 format.
-- Unknown: The encoding type is unknown or invalid. The data can be treated as binary.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeDatabase [<Switch>]
If this switch is enabled, mappings for databases will not be exported.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeJobs [<Switch>]
If this switch is enabled, Agent job ownership will not be exported.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeLogin [<System.Object[]>]
The login(s) to exclude. Options for this list are auto-populated from the server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludePassword [<Switch>]
If this switch is enabled, hashed passwords will not be exported.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FilePath [<String>]
Specifies the full file path of the output file. If left blank then filename based on Instance name and date
is created.
If more than one instance is input then this parameter should be blank.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-InputObject [<System.Object[]>]
Enables piping from Get-DbaDatabase, Get-DbaLogin and more.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Login [<System.Object[]>]
The login(s) to process. Options for this list are auto-populated from the server. If unspecified, all logins
will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoClobber [<Switch>]
If this switch is enabled, a file already existing at the path specified by Path will not be overwritten.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoPrefix [<Switch>]
Do not include a Prefix
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Passthru [<Switch>]
Output script to console
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Path [<String>]
Specifies the directory where the file or files will be exported.
Will default to Path.DbatoolsExport Configuration entry
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<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.
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. SQL Server 2000 and above supported.
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: Export, Login
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:\\>Export-DbaLogin -SqlInstance sql2005 -Path C:\\temp\\sql2005-logins.sql
Exports the logins for SQL Server "sql2005" and writes them to the file "C:\\temp\\sql2005-logins.sql"
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2014a -ExcludeLogin realcajun -SqlCredential $scred -Path
C:\\temp\\logins.sql -Append
Authenticates to sqlserver2014a using SQL Authentication. Exports all logins except for realcajun to
C:\\temp\\logins.sql, and appends to the file if it exists. If not, the file will be created.
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2014a -Login realcajun, netnerds -Path C:\\temp\\logins.sql
Exports ONLY logins netnerds and realcajun FROM sqlserver2014a to the file C:\\temp\\logins.sql
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2014a -Login realcajun, netnerds -Database HR, Accounting
Exports ONLY logins netnerds and realcajun FROM sqlserver2014a with the permissions on databases HR and Accounting
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR, Accounting | Export-DbaLogin
Exports ONLY logins FROM sqlserver2014a with permissions on databases HR and Accounting
-------------------------- EXAMPLE 6 --------------------------
PS C:\\>Set-DbatoolsConfig -FullName formatting.batchseparator -Value $null
PS C:\\> Export-DbaLogin -SqlInstance sqlserver2008 -Login realcajun, netnerds -Path C:\\temp\\login.sql
Exports ONLY logins netnerds and realcajun FROM sqlserver2008 server, to the C:\\temp\\login.sql file without the
'GO' batch separator.
-------------------------- EXAMPLE 7 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2008 -Login realcajun -Path C:\\temp\\users.sql -DestinationVersion
SQLServer2016
Exports login realcajun from sqlserver2008 to the file C:\\temp\\users.sql with syntax to run on SQL Server 2016
-------------------------- EXAMPLE 8 --------------------------
PS C:\\>Get-DbaDatabase -SqlInstance sqlserver2008 -Login realcajun | Export-DbaLogin
Exports login realcajun from sqlserver2008
-------------------------- EXAMPLE 9 --------------------------
PS C:\\>Get-DbaLogin -SqlInstance sqlserver2008, sqlserver2012 | Where-Object { $_.IsDisabled -eq $false } |
Export-DbaLogin
Exports all enabled logins from sqlserver2008 and sqlserver2008
RELATED LINKS
https://dbatools.io/Export-DbaLogin
SYNOPSIS
Exports Windows and SQL Logins to a T-SQL file. Export includes login, SID, password, default database, default
language, server permissions, server roles, db permissions, db roles.
SYNTAX
Export-DbaLogin [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]
<Pscredential>] [[-InputObject] <System.Object[]>] [[-Login] <System.Object[]>] [[-ExcludeLogin]
<System.Object[]>] [[-Database] <System.Object[]>] [-ExcludeJobs <Switch>] [-ExcludeDatabase <Switch>]
[-ExcludePassword <Switch>] [[-DefaultDatabase] <String>] [[-Path] <String>] [[-FilePath] <String>] [[-Encoding]
<String>] [-NoClobber <Switch>] [-Append <Switch>] [[-BatchSeparator] <String>] [[-DestinationVersion] <String>]
[-NoPrefix <Switch>] [-Passthru <Switch>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Exports Windows and SQL Logins to a T-SQL file. Export includes login, SID, password, default database, default
language, server permissions, server roles, db permissions, db roles.
PARAMETERS
-Append [<Switch>]
If this switch is enabled, content will be appended to a file already existing at the path specified by Path.
If the file does not exist, it will be created.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSeparator [<String>]
Batch separator for scripting output. Uses the value from configuration Formatting.BatchSeparator by default.
This is normally "GO"
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all
databases will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DefaultDatabase [<String>]
If this switch is enabled, all logins will be scripted with specified default database,
that could help to successfully import logins on server that is missing default database for login.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationVersion [<String>]
To say to which version the script should be generated. If not specified will use instance major version.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Encoding [<String>]
Specifies the file encoding. The default is UTF8.
Valid values are:
-- ASCII: Uses the encoding for the ASCII (7-bit) character set.
-- BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order.
-- Byte: Encodes a set of characters into a sequence of bytes.
-- String: Uses the encoding type for a string.
-- Unicode: Encodes in UTF-16 format using the little-endian byte order.
-- UTF7: Encodes in UTF-7 format.
-- UTF8: Encodes in UTF-8 format.
-- Unknown: The encoding type is unknown or invalid. The data can be treated as binary.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeDatabase [<Switch>]
If this switch is enabled, mappings for databases will not be exported.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeJobs [<Switch>]
If this switch is enabled, Agent job ownership will not be exported.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeLogin [<System.Object[]>]
The login(s) to exclude. Options for this list are auto-populated from the server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludePassword [<Switch>]
If this switch is enabled, hashed passwords will not be exported.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FilePath [<String>]
Specifies the full file path of the output file. If left blank then filename based on Instance name and date
is created.
If more than one instance is input then this parameter should be blank.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-InputObject [<System.Object[]>]
Enables piping from Get-DbaDatabase, Get-DbaLogin and more.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Login [<System.Object[]>]
The login(s) to process. Options for this list are auto-populated from the server. If unspecified, all logins
will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoClobber [<Switch>]
If this switch is enabled, a file already existing at the path specified by Path will not be overwritten.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoPrefix [<Switch>]
Do not include a Prefix
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Passthru [<Switch>]
Output script to console
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Path [<String>]
Specifies the directory where the file or files will be exported.
Will default to Path.DbatoolsExport Configuration entry
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<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.
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. SQL Server 2000 and above supported.
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: Export, Login
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:\\>Export-DbaLogin -SqlInstance sql2005 -Path C:\\temp\\sql2005-logins.sql
Exports the logins for SQL Server "sql2005" and writes them to the file "C:\\temp\\sql2005-logins.sql"
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2014a -ExcludeLogin realcajun -SqlCredential $scred -Path
C:\\temp\\logins.sql -Append
Authenticates to sqlserver2014a using SQL Authentication. Exports all logins except for realcajun to
C:\\temp\\logins.sql, and appends to the file if it exists. If not, the file will be created.
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2014a -Login realcajun, netnerds -Path C:\\temp\\logins.sql
Exports ONLY logins netnerds and realcajun FROM sqlserver2014a to the file C:\\temp\\logins.sql
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2014a -Login realcajun, netnerds -Database HR, Accounting
Exports ONLY logins netnerds and realcajun FROM sqlserver2014a with the permissions on databases HR and Accounting
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR, Accounting | Export-DbaLogin
Exports ONLY logins FROM sqlserver2014a with permissions on databases HR and Accounting
-------------------------- EXAMPLE 6 --------------------------
PS C:\\>Set-DbatoolsConfig -FullName formatting.batchseparator -Value $null
PS C:\\> Export-DbaLogin -SqlInstance sqlserver2008 -Login realcajun, netnerds -Path C:\\temp\\login.sql
Exports ONLY logins netnerds and realcajun FROM sqlserver2008 server, to the C:\\temp\\login.sql file without the
'GO' batch separator.
-------------------------- EXAMPLE 7 --------------------------
PS C:\\>Export-DbaLogin -SqlInstance sqlserver2008 -Login realcajun -Path C:\\temp\\users.sql -DestinationVersion
SQLServer2016
Exports login realcajun from sqlserver2008 to the file C:\\temp\\users.sql with syntax to run on SQL Server 2016
-------------------------- EXAMPLE 8 --------------------------
PS C:\\>Get-DbaDatabase -SqlInstance sqlserver2008 -Login realcajun | Export-DbaLogin
Exports login realcajun from sqlserver2008
-------------------------- EXAMPLE 9 --------------------------
PS C:\\>Get-DbaLogin -SqlInstance sqlserver2008, sqlserver2012 | Where-Object { $_.IsDisabled -eq $false } |
Export-DbaLogin
Exports all enabled logins from sqlserver2008 and sqlserver2008
RELATED LINKS
https://dbatools.io/Export-DbaLogin