DBA Scripts: Get Sysadmin Members

🔧Part of the DBA-Tools Project, copy/paste SQL Server scripts and health checks.In: Security

The sysadmin fixed server role gives unrestricted access to a SQL Server instance: any operation, all data, every security setting. Knowing exactly who holds it is a basic but important security review.

In many environments membership grows over time. Temporary access becomes permanent, old administrator accounts stay enabled, service accounts are granted too much, and accounts from previous teams are never removed.

This script returns every member of sysadmin with its account type, disabled status and creation details. A clean list should never contain surprises: if you do not recognise every account, you have something to investigate.


The Script

Run the following script against your SQL Server instance. It is read-only and needs VIEW ANY DEFINITION, which sysadmin and securityadmin hold already.

/*
Script Name : Get-SysadminMembers
Category    : security-and-permissions
Purpose     : List members of the sysadmin fixed server role.
Author      : Peter Whyte (https://sqldba.blog/dba-scripts-get-sysadmin-members/)
Requires    : VIEW ANY DEFINITION (sysadmin and securityadmin have it implicitly)
HealthCheck : Yes
Related     : Get-ServerRoleMembers, the same check across every server role
*/
-- SAFE:ReadOnly
-- IMPACT:Low
SET NOCOUNT ON;

SELECT
    sp.name AS login_name,
    sp.type_desc,
    sp.is_disabled,
    sp.create_date,
    sp.modify_date
FROM sys.server_principals sp
JOIN sys.server_role_members srm ON sp.principal_id = srm.member_principal_id
JOIN sys.server_principals sr ON srm.role_principal_id = sr.principal_id
WHERE sr.name = 'sysadmin'
ORDER BY sp.name;

Example Output

Below is the script run against a test instance. Eleven principals hold sysadmin: five SQL logins, five Windows service accounts created by the SQL Server installer, and sa. Row 4 is the one to notice. DemoDisabledAdmin carries is_disabled = 1 and still holds the role.

SSMS results grid showing Get-SysadminMembers returning 11 members of the SQL Server sysadmin fixed server role, with login_name, type_desc, is_disabled, create_date and modify_date columns. Five are SQL logins and five are Windows service logins, and DemoDisabledAdmin has is_disabled set to 1.
Eleven sysadmin members on a test instance, including one disabled account.

The status bar reads 11 rows, and that number matters more than it looks. A short result is the thing to be suspicious of, for the reason set out below.

Run it from the DBA Tools repo
git clone https://github.com/peterwhyte-lgtm/dba-tools
cd dba-tools
.\Initialize-Environment.ps1
.\run.ps1 Get-SysadminMembers

Add -ServerInstance SQLSERVER01 to run against another server. Script files: Get-SysadminMembers.sql · Get-SysadminMembers.ps1


Understanding the Results

One row per principal that is a direct member of sysadmin. Five columns, and the last two are more useful than they look.

login_name
The principal that holds sysadmin. In the run above that is five SQL logins, five Windows service accounts and sa.WatchEvery name here should map to a person or a documented service. A name nobody can account for is the finding.
type_desc
Taken straight from sys.server_principals.type_desc and not filtered, so a value you do not recognise is a real value and not a bug.Blind spotA WINDOWS_GROUP row is a group, not a person. Its members never appear here and have to be reviewed in Active Directory.
is_disabled
1 means the login cannot authenticate today. DemoDisabledAdmin above is the example.WatchDisabled is not removed. Re-enabling is one command and the sysadmin membership comes back with it, so review and justify rather than assuming it is harmless.
create_date
When the login was created. Not when it was granted sysadmin, which is a different and usually more interesting date.
modify_date
When the login was last changed. Measured on SQL Server 2025: this does move when server role membership changes, so on a sysadmin member a modify_date well after the create_date is often the moment the role was granted.WatchA recent modify_date on a privileged login is a reasonable thing to ask a question about.

What This Script Does Not Tell You

Read this before using the output as evidence. The query answers one narrow question: which server principals are direct members of sysadmin.

Scope limits
01
It can under-report, and it does so silently
Visibility of sys.server_principals is itself permission controlled. A caller without VIEW ANY DEFINITION sees only the principals it owns and gets a shorter list rather than an error. On my test instance the full list is 11 rows. The identical query run as a plain login with no server level permission returned 1. No warning, just an instance that looks clean. Confirm the result is complete before you trust it:
SELECT IS_SRVROLEMEMBER('sysadmin') AS is_sysadmin,
       HAS_PERMS_BY_NAME(NULL, NULL, 'VIEW ANY DEFINITION') AS can_see_all_principals;
If can_see_all_principals is 0 and you are not a sysadmin, the list is incomplete. VIEW ANY DATABASE is granted to public and does not help here, which is exactly why it is the wrong thing to check for.
02
It does not expand Windows groups
If DOMAIN\DBA-Admins appears, that group holds sysadmin. The query shows the group and never the people inside it. To answer “who actually has sysadmin on this instance” you have to review that group in Active Directory too, and again every time its membership changes.
03
It does not show access earned another way
CONTROL SERVER, ownership chaining, high privilege database roles and impersonation can add up to something close to administrative control without putting a row in this list.
04
It does not tell you whether the access is justified
Or whether it was ever used. That is the review, and it is the part only you can do. The title says sysadmin members, not everyone who can administer this instance, and the difference matters when the output goes to an auditor.

What to Do With the List

  • Keep sysadmin small, prefer Windows groups over individual accounts, and keep application service accounts out of it.
  • Disable sa if nothing uses it. It is the one sysadmin login every attacker already knows the name of. Check nothing connects as sa first (the login activity session shows who logs in), then run ALTER LOGIN sa DISABLE;.
  • Document why each member exists, and treat every new sysadmin member as a change needing approval.
Docs: Microsoft’s reference covers sys.server_principals and sys.server_role_members in full.

Frequently Asked Questions

Why does the script return fewer rows than I expect?

Almost always permissions. Seeing every login needs ALTER ANY LOGIN, which securityadmin holds, or VIEW ANY DEFINITION, or sysadmin (MS Docs). Without it SQL Server returns only the principals you can see instead of raising an error, so the result looks like a short, healthy list. Run the HAS_PERMS_BY_NAME check above before trusting a small number.

Why don’t I see members of Windows groups?

SQL Server stores only the Windows group as the sysadmin member. Individual users are resolved by Active Directory, so you’ll need to inspect the group’s membership outside SQL Server.

What is the sysadmin role in SQL Server?

sysadmin is a fixed server role that provides unrestricted administrative access to a SQL Server instance. Members can perform any action on the server.

Should application accounts have sysadmin access?

Generally no.

Application accounts should normally have only the permissions required for the application workload.

Should every DBA be a sysadmin?

DBAs typically require elevated access, but membership should still be controlled, documented and reviewed regularly.


Related

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *