< Back

New-DbaDbUser

Mon Jan 13, 2020 12:56 pm

NAME New-DbaDbUser



SYNOPSIS

Creates a new user for the specified database.





SYNTAX

New-DbaDbUser [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [-SqlCredential

<Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>] [-IncludeSystem <Switch>]

[-Username <System.String[]>] [-Force <Switch>] [-EnableException <Switch>] [<CommonParameters>]



New-DbaDbUser [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [-SqlCredential

<Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>] [-IncludeSystem <Switch>]

[-Login <String>] [-Username <System.String[]>] [-Force <Switch>] [-EnableException <Switch>] [<CommonParameters>]





DESCRIPTION

Creates a new user for a specified database with provided specifications.





PARAMETERS

-Database [<System.Object[]>]

Specifies 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



-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



-ExcludeDatabase [<System.Object[]>]

Specifies the database(s) to exclude from processing. Options for this list are auto-populated from the

server. By default, system databases are excluded.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Force [<Switch>]

If user exists, drop and recreate.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-IncludeSystem [<Switch>]

If this switch is enabled, the user will be added to system databases.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Login [<String>]

When specified, the user will be associated to this SQL login and have the same name as the Login.



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. Defaults to the default instance on localhost.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Username [<System.String[]>]

When specified, the user will have this name.



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: Database, User

Author: Frank Henninger (@osiris687)



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



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



Creates a new sql user with login named user1 in the specified database.

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -Username user1



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

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>New-DbaDbUser -SqlInstance sqlserver2014 -Database DB1 -Login Login1 -Username user1



Creates a new sql user named user1 mapped to Login1 in the specified database.

-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Get-DbaDbUser -SqlInstance sqlserver1 -Database DB1 | New-DbaDbUser -SqlInstance sqlserver2 -Database DB1



Copies users from sqlserver1.DB1 to sqlserver2.DB1. Does not copy permissions!



RELATED LINKS

https://dbatools.io/New-DbaDbUser