Author | Claudio Silva (@ClaudioESSilva) |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Export-DbaUser on GitHub.
Want to see the Bill Of Health for this command? Check out Export-DbaUser.
Exports users creation and its permissions to a T-SQL file or host.
Exports users creation and its permissions to a T-SQL file or host. Export includes user, create and add to role(s), database level permissions, object level permissions.
Export-DbaUser
[[-SqlInstance] <DbaInstanceParameter[]>]
[[-InputObject] <Database[]>]
[[-SqlCredential] <PSCredential>]
[[-Database] <String[]>]
[[-ExcludeDatabase] <String[]>]
[[-User] <String[]>]
[[-DestinationVersion] <String>]
[[-Path] <String>]
[[-FilePath] <String>]
[[-Encoding] <String>]
[-NoClobber]
[-Append]
[-Passthru]
[-Template]
[-EnableException]
[[-ScriptingOptionsObject] <ScriptingOptions>]
[-ExcludeGoBatchSeparator]
[<CommonParameters>]
PS C:\> Export-DbaUser -SqlInstance sql2005 -FilePath C:\temp\sql2005-users.sql
Exports SQL for the users in server "sql2005" and writes them to the file "C:\temp\sql2005-users.sql"
PS C:\> Export-DbaUser -SqlInstance sqlserver2014a $scred -FilePath C:\temp\users.sql -Append
Authenticates to sqlserver2014a using SQL Authentication. Exports all users to C:\temp\users.sql, and appends to the file if it exists. If not, the file will be created.
PS C:\> Export-DbaUser -SqlInstance sqlserver2014a -User User1, User2 -FilePath C:\temp\users.sql
Exports ONLY users User1 and User2 from sqlserver2014a to the file C:\temp\users.sql
PS C:\> Export-DbaUser -SqlInstance sqlserver2014a -User User1, User2 -Path C:\temp
Exports ONLY users User1 and User2 from sqlserver2014a to the folder C:\temp. One file per user will be generated
PS C:\> Export-DbaUser -SqlInstance sqlserver2008 -User User1 -FilePath C:\temp\users.sql -DestinationVersion SQLServer2016
Exports user User1 from sqlserver2008 to the file C:\temp\users.sql with syntax to run on SQL Server 2016
PS C:\> Export-DbaUser -SqlInstance sqlserver2008 -Database db1,db2 -FilePath C:\temp\users.sql
Exports ONLY users from db1 and db2 database on sqlserver2008 server, to the C:\temp\users.sql file.
PS C:\> $options = New-DbaScriptingOption
PS C:\> $options.ScriptDrops = $false
PS C:\> $options.WithDependencies = $true
PS C:\> Export-DbaUser -SqlInstance sqlserver2008 -Database db1,db2 -FilePath C:\temp\users.sql -ScriptingOptionsObject $options
Exports ONLY users from db1 and db2 database on sqlserver2008 server, to the C:\temp\users.sql file.
It will not script drops but will script dependencies.
PS C:\> Export-DbaUser -SqlInstance sqlserver2008 -Database db1,db2 -FilePath C:\temp\users.sql -ExcludeGoBatchSeparator
Exports ONLY users from db1 and db2 database on sqlserver2008 server, to the C:\temp\users.sql file without the 'GO' batch separator.
PS C:\> Export-DbaUser -SqlInstance sqlserver2008 -Database db1 -User user1 -Template -PassThru
Exports user1 from database db1, replacing loginname and username with and correspondingly.
The target SQL Server instance or instances. SQL Server 2000 and above supported.
Alias | |
Required | False |
Pipeline | true (ByValue) |
Default Value |
Allows database objects to be piped in from Get-DbaDatabase
Alias | |
Required | False |
Pipeline | true (ByValue) |
Default Value |
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.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The database(s) to process - this list is auto-populated from the server. If unspecified, all InputObject will be processed.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The database(s) to exclude - this list is auto-populated from the server
Alias | |
Required | False |
Pipeline | false |
Default Value |
Export only the specified database user(s). If not specified will export all users from the database(s)
Alias | |
Required | False |
Pipeline | false |
Default Value |
To say to which version the script should be generated. If not specified will use database compatibility level
Alias | |
Required | False |
Pipeline | false |
Default Value | |
Accepted Values | SQLServer2000,SQLServer2005,SQLServer2008/2008R2,SQLServer2012,SQLServer2014,SQLServer2016,SQLServer2017,SQLServer2019,SQLServer2022 |
Specifies the directory where the file or files will be exported.
Alias | |
Required | False |
Pipeline | false |
Default Value | (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport') |
Specifies the full file path of the output file.
Alias | OutFile,FileName |
Required | False |
Pipeline | false |
Default Value |
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.
Alias | |
Required | False |
Pipeline | false |
Default Value | UTF8 |
Accepted Values | ASCII,BigEndianUnicode,Byte,String,Unicode,UTF7,UTF8,Unknown |
Do not overwrite file
Alias | NoOverwrite |
Required | False |
Pipeline | false |
Default Value | False |
Append to file
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Output script to console, useful with | clip
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Script user as a templated string that contains tokens and instead of username and login
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
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.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
A Microsoft.SqlServer.Management.Smo.ScriptingOptions object with the options that you want to use to generate the t-sql script.
You can use the New-DbaScriptingOption to generate it.
Alias | |
Required | False |
Pipeline | false |
Default Value |
If specified, will NOT script the 'GO' batch separator.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |