dbatools is a free PowerShell module with over 300 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


Set-DbaLogin

Set-DbaLogin will enable you to change the password, unlock, rename, disable or enable, deny or grant login privileges to the login. It’s also possible to add or remove server roles from the login.

Screenshots

dbatools-Set-DbaLogin

Examples

Set the new password for login1 using a credential, unlock the account and set the option that the user must change password at next logon.

$cred = Get-Credential
Set-DbaLogin -SqlInstance sql1 -Login login1 -Password $cred -Unlock -MustChange

Enable the login.

Set-DbaLogin -SqlInstance sql1 -Login login1 -Enable

Enable multiple logins.

Set-DbaLogin -SqlInstance sql1 -Login login1, login2, login3, login4 -Enable

Enable multiple logins on multiple instances.

Set-DbaLogin -SqlInstance sql1, sql2, sql3 -Login login1, login2, login3, login4 -Enable

Disable the login.

Set-DbaLogin -SqlInstance sql1 -Login login1 -Disable

Deny the login to connect to the instance.

Set-DbaLogin -SqlInstance sql1 -Login login1 -DenyLogin

Grant the login to connect to the instance.

Set-DbaLogin -SqlInstance sql1 -Login login1 -GrantLogin

Add the server role “serveradmin” to the login.

Set-DbaLogin -SqlInstance sql1 -Login test -AddRole serveradmin

Remove the server role “bulkadmin” to the login.

Set-DbaLogin -SqlInstance sql1 -Login test -RemoveRole bulkadmin

Author

This command was created by Sander Stad. You can find Sander on Twitter.

Get-Help

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

Source Code

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

Related commands