commands

^

New-DbaDbUser

Author Frank Henninger (@osiris687) , Andreas Jordan (@JordanOrdix), ordix.de
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out New-DbaDbUser on GitHub.
Want to see the Bill Of Health for this command? Check out New-DbaDbUser.

Synopsis

Creates a new user for the specified database(s).

Description

Creates a new user for the specified database(s) with provided specifications.

Syntax

New-DbaDbUser
    [-SqlInstance] <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Database <String[]>]
    [-ExcludeDatabase <String[]>]
    [-IncludeSystem]
    -User <String>
    -ExternalProvider
    [-DefaultSchema <String>]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

New-DbaDbUser
    [-SqlInstance] <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Database <String[]>]
    [-ExcludeDatabase <String[]>]
    [-IncludeSystem]
    -User <String>
    -SecurePassword <SecureString>
    [-DefaultSchema <String>]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

New-DbaDbUser
    [-SqlInstance] <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Database <String[]>]
    [-ExcludeDatabase <String[]>]
    [-IncludeSystem]
    -User <String>
    [-DefaultSchema <String>]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

New-DbaDbUser
    [-SqlInstance] <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Database <String[]>]
    [-ExcludeDatabase <String[]>]
    [-IncludeSystem]
    [-User <String>]
    -Login <String>
    [-DefaultSchema <String>]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -Login user1

Creates a new sql user named user1 for the login user1 in the database DB1.

Example: 2
PS C:\> New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -User user1

Creates a new sql user named user1 without login in the database DB1.

Example: 3
PS C:\> New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -User user1 -Login login1

Creates a new sql user named user1 for the login login1 in the database DB1.

Example: 4
PS C:\> New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -User user1 -Login Login1 -DefaultSchema schema1

Creates a new sql user named user1 for the login login1 in the database DB1 and specifies the default schema to be schema1.

Example: 5
PS C:\> New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -User "claudio@********.onmicrosoft.com" -ExternalProvider

Creates a new sql user named 'claudio@********.onmicrosoft.com' mapped to Azure Active Directory (AAD) in the database DB1.

Example: 6
PS C:\> New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -Username user1 -Password (ConvertTo-SecureString -String "DBATools" -AsPlainText -Force)

Creates a new contained sql user named user1 in the database DB1 with the password specified.

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

Alias
Required True
Pipeline false
Default Value
-User

When specified, the user will have this name. If not specified but -Login is used, the user will have the same name as the login.

Alias Username
Required True
Pipeline false
Default Value
-Login

When specified, the user will be associated to this SQL login.

Alias
Required True
Pipeline false
Default Value
-SecurePassword

If we need to pass a password to the command, we always use the type securestring and name the parameter SecurePassword. Here we only use the alias for backwords compatibility.

Alias Password
Required True
Pipeline false
Default Value
-ExternalProvider

When specified, the user will be created for Azure AD Authentication.
Equivalent to T-SQL: 'CREATE USER [claudio@********.onmicrosoft.com] FROM EXTERNAL PROVIDER`

Alias
Required True
Pipeline false
Default Value False

Optional Parameters

-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

Specifies one or more database(s) to process. If unspecified, all user databases will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

Specifies one or more database(s) to exclude from processing.

Alias
Required False
Pipeline false
Default Value
-IncludeSystem

If this switch is enabled, also system databases will be processed.

Alias
Required False
Pipeline false
Default Value False
-DefaultSchema

The default database schema for the user. If not specified this value will default to dbo.

Alias
Required False
Pipeline false
Default Value dbo
-Force

If user exists, drop and recreate.

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

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

Alias cf
Required False
Pipeline false
Default Value