commands

^

Export-DbaUser

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.

Synopsis

Exports users creation and its permissions to a T-SQL file or host.

Description

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.

Syntax

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>]

 

Examples

 

Example: 1
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"

Example: 2
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.

Example: 3
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

Example: 4
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

Example: 5
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

Example: 6
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.

Example: 7
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.

Example: 8
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.

Example: 9
PS C:\> Export-DbaUser -SqlInstance sqlserver2008 -Database db1 -User user1 -Template -PassThru

Exports user1 from database db1, replacing loginname and username with and correspondingly.

Optional Parameters

-SqlInstance

The target SQL Server instance or instances. SQL Server 2000 and above supported.

Alias
Required False
Pipeline true (ByValue)
Default Value
-InputObject

Allows database objects to be piped in from Get-DbaDatabase

Alias
Required False
Pipeline true (ByValue)
Default Value
-SqlCredential

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
-Database

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
-ExcludeDatabase

The database(s) to exclude - this list is auto-populated from the server

Alias
Required False
Pipeline false
Default Value
-User

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
-DestinationVersion

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
-Path

Specifies the directory where the file or files will be exported.

Alias
Required False
Pipeline false
Default Value (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport')
-FilePath

Specifies the full file path of the output file.

Alias OutFile,FileName
Required False
Pipeline false
Default Value
-Encoding

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
-NoClobber

Do not overwrite file

Alias NoOverwrite
Required False
Pipeline false
Default Value False
-Append

Append to file

Alias
Required False
Pipeline false
Default Value False
-Passthru

Output script to console, useful with | clip

Alias
Required False
Pipeline false
Default Value False
-Template

Script user as a templated string that contains tokens and instead of username and login

Alias
Required False
Pipeline false
Default Value False
-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.

Alias
Required False
Pipeline false
Default Value False
-ScriptingOptionsObject

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
-ExcludeGoBatchSeparator

If specified, will NOT script the 'GO' batch separator.

Alias
Required False
Pipeline false
Default Value False