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


Copy-SqlLogin

This command migrates logins from source to destination SQL Servers. Supports SQL Server versions 2000 and above. Migrates logins with SIDs, passwords, defaultdb, server roles & securables, database permissions & securables, login attributes (enforce password policy, expiration, etc).

By default, all logins with the exception of system (####Example Login## and local (SERVERNAME\administrators) logins are copied. The -Logins parameter is autopopulated for command-line completion and can be used to copy only specific logins.

If the login already exists on the destination, it will be skipped unless -Force is specified. Force drops and recreates the login.

Examples

To copy all logins from sqlserver to sqlcluster, using integrated (Windows) login. If logins with the same name exist on sqlcluster, they will be skipped.

To copy only two SQL logins, netnerds and realcajun, as well as one Windows login ‘base\ctrlb’

To copy all logins from sqlserver to sqlcluster, using SQL credentials to login to the destination, sqlcluster. If logins with the same name exist on sqlcluster, they will be dropped and recreated because Force was specified.

Watch Copy-SqlLogin in action.

From PowerShell, execute   Get-Help Copy-SqlLogin -Detailed   for more information on this function. Want to see the source code? See it on GitHub