dbatools is a free PowerShell module with over 200 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


New-DbaLogin

Creates a new SQL Server login with provided specifications.

Screenshots

dbatools-New-DbaLogin

Examples

You will be prompted to securely enter the password for a login [Newlogin]. The login would be created on servers Server1 and Server2 with default parameters.

New-DbaLogin -SqlInstance Server1,Server2 -Login Newlogin

Creates a login on Server1\sql1 with a predefined password. The login will have password and expiration policies enforced onto it.

$securePassword = Read-Host “Input password” -AsSecureString
New-DbaLogin -SqlInstance Server1\sql1 -Login Newlogin -Password $securePassword -PasswordPolicy -PasswordExpiration

Copies a login [Oldlogin] to the same instance sql1 with the same parameters (including password). New login will have a new sid, a new name [Newlogin] and will not be disabled. Existing login [Newlogin] will be removed prior to creation.

Get-DbaLogin -SqlInstance sql1 -Login Oldlogin | New-DbaLogin -SqlInstance sql1 -LoginRenameHashtable @{Oldlogin = ‘Newlogin’} -Force -NewSid -Disabled:$false

Copies logins [Login1] and [Login2] from instance sql1 to instance sql2, but enforces password and expiration policies for the new logins. New logins will also have a default database set to [tempdb] and will be created in a disabled state.

Get-DbaLogin -SqlInstance sql1 -Login Login1,Login2 | New-DbaLogin -SqlInstance sql2 -PasswordPolicy -PasswordExpiration -DefaultDatabase tempdb -Disabled

Author

This command was created by Kirill Kravtsov. You can find Kirill on Twitter.

Get-Help

From PowerShell, execute Get-Help New-DbaLogin -Detailed for more information on this function.

Source Code

Want to see the source code? View New-DbaLogin.ps1 on GitHub
 

Related commands