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


Exports Windows and SQL Logins to a T-SQL file. 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 exported.

The -Logins and -Exclude parameters are autopopulated for command-line completion and can be used to export only specific logins or exclude specific logins.





To export SQL for the logins in server “sql2005” and write them to the file “C:\temp\sql2005-logins.sql”

Export-SqlLogin -SqlServer sql2005 -FilePath C:\temp\sql2005-logins.sql

To authenticate to sqlserver2014a using SQL Authentication. Exports all logins except for realcajun to C:\temp\logins.sql, and appends to the file if it exists. If not, the file will be created.

Export-SqlLogin -SqlServer sqlserver2014a -Exclude realcajun -SqlCredential $scred -FilePath C:\temp\logins.sql -Append

To export ONLY logins netnerds and realcajun fron sqlsever2014a to the file C:\temp\logins.sql

Export-SqlLogin -SqlServer sqlserver2014a -Login realcajun, netnerds -FilePath C:\temp\logins.sql

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