Home / Free tools / SQL login script generator

SQL Server login script generator (SID and password hash)

Copy SQL Server logins to another instance with their original SID and password, without installing sp_help_revlogin. Run one query on the source, paste its results here and get the CREATE LOGIN ... WITH PASSWORD = 0x... HASHED, SID = 0x... script to run on the destination.

Runs only in your browser. The results you paste never leave this page: they are not sent to our server, not logged, not stored and not put in a URL or in analytics. You can disconnect from the network after the page has loaded and it still works. Close the tab when you are done.

1. Run this query on the source instance

Connect as a member of sysadmin (without it SQL Server hides the password hashes). In SSMS choose Results to Grid, run the query, click in the results, then press Ctrl+A and Ctrl+C.

USE [master];
SELECT
    p.type AS login_type,
    CONVERT(varchar(100), p.sid, 1) AS sid_hex,
    ISNULL(CONVERT(varchar(600), CAST(LOGINPROPERTY(p.name, 'PasswordHash') AS varbinary(256)), 1), '') AS password_hash,
    CONVERT(int, p.is_disabled) AS is_disabled,
    ISNULL(CONVERT(int, l.is_policy_checked), 0) AS check_policy,
    ISNULL(CONVERT(int, l.is_expiration_checked), 0) AS check_expiration,
    ISNULL((SELECT TOP (1) sp.state FROM sys.server_permissions AS sp
            WHERE sp.grantee_principal_id = p.principal_id AND sp.type = 'COSQ'), 'N') AS connect_sql,
    ISNULL(p.default_database_name, 'master') AS default_database,
    ISNULL(p.default_language_name, '') AS default_language,
    p.name AS login_name
FROM sys.server_principals AS p
LEFT JOIN sys.sql_logins AS l ON l.principal_id = p.principal_id
WHERE p.type IN ('S', 'U', 'G')      -- SQL logins, Windows logins, Windows groups
  AND p.sid <> 0x01                  -- sa, even when renamed
  AND p.name NOT LIKE '##%'
  AND p.name NOT LIKE 'NT SERVICE\%'
  AND p.name NOT LIKE 'NT AUTHORITY\%'
ORDER BY p.name;

2. Paste the results

Options

3. Review the script and run it on the destination

The script holds every SQL login's password hash: treat it like a password file and do not leave it in a ticket, a share or a repository.

What the script does

  • SQL logins are created with PASSWORD = 0x... HASHED and the original SID, so the password stays the same and the users in restored databases match their login again (no orphaned users).
  • Windows logins and groups are created FROM WINDOWS. Their SID comes from Windows, so it is the same on every server in the domain.
  • The default database and language, CHECK_POLICY and CHECK_EXPIRATION are copied. With the third option, a disabled login gets ALTER LOGIN ... DISABLE and a login denied access gets DENY CONNECT SQL.
  • The query leaves out sa (whatever it is called), the ## certificate logins and NT SERVICE\ / NT AUTHORITY\ accounts, which belong to each instance.
  • Every name is quoted, so names with spaces, ] or ' work.

What it does not copy

Like sp_help_revlogin, this copies the logins only. Server role membership (sysadmin, dbcreator, user-defined server roles), server-level permissions (VIEW SERVER STATE, ALTER ANY LOGIN ...), credentials and SQL Agent jobs stay behind. If a login's default database is not on the destination yet, its CREATE LOGIN fails (Msg 15010): restore the databases first, or change DEFAULT_DATABASE to master in the script. A login that already exists under the same name is skipped, even if its SID is different. The password hash format follows the source (0x0200 on SQL Server 2012 to 2022, 0x0300 on SQL Server 2025 or 2022 with trace flag 4671); the guide to transferring logins covers the version rules, fixing orphaned users and checking both servers afterwards.

Questions

Is it safe to paste password hashes into this page?

The page never sends what you paste anywhere: the script is built by JavaScript in your browser, and nothing is logged, stored or put in a URL or analytics. You can load the page, disconnect from the network and it still works. The output still holds the hashes, so handle it like a password file.

Is this the same as sp_help_revlogin?

It produces the same kind of script (CREATE LOGIN with PASSWORD = 0x... HASHED and the original SID) without creating the sp_hexadecimal and sp_help_revlogin procedures in master on the source server. You run one read-only query instead.

Why do my database users become orphaned after a restore?

A database user points at its login by SID. A login created again by hand gets a new SID, so the restored user no longer matches it. Creating the login with its original SID, as this script does, keeps them matched. Users that are already orphaned can be fixed with ALTER USER ... WITH LOGIN, see the transfer logins guide.

Why is the password hash column empty?

SQL Server shows password hashes only to a login with CONTROL SERVER (members of sysadmin have it). Run the query as a sysadmin. Logins without a hash are skipped and listed under the script.

Can I copy logins from a newer SQL Server version to an older one?

A 0x0200 hash (SQL Server 2012 to 2022) is the usual case. SQL Server 2025 writes a 0x0300 hash (PBKDF2), and Microsoft says versions that do not support it cannot authenticate the login, so copying such a login to an older instance means setting its password again there. The first bytes of the password_hash column tell you which one you have.

Does it copy server roles and permissions?

No. It copies the logins (name, SID, password, default database and language, policy flags, disabled and denied state). Server role membership and server-level permissions have to be copied separately; SQLParity compares them across servers.

Something wrong or missing? Mail support@solutionsgroupunlimited.com. More free tools: cron expression builder, GUID / UUID generator. How-to articles: Guides.

Sources (Microsoft Learn): Transfer logins and passwords between instances of SQL Server, CREATE LOGIN, LOGINPROPERTY, sys.server_principals.

An unhandled error has occurred. 🗙