Script to find SQL Server service account

SQL Server service account information can be fetched from registry or from sys.dm_server_services for versions starting from SQL Server 2008 R2 SP1 and above. This blog provides script to find SQL Server service account.

Execute below query to get the service account of current SQL Server installation from the registry.

DECLARE @DatabaseEngineAccount VARCHAR(100) 
DECLARE @SQLAgentAccount VARCHAR(100) 

EXECUTE Xp_instance_regread 
  @rootkey = N'HKEY_LOCAL_MACHINE', 
  @key = N'SYSTEM\CurrentControlSet\Services\MSSQLServer', 
  @value_name = N'ObjectName', 
  @value = @DatabaseEngineAccount output 

EXECUTE Xp_instance_regread 
  @rootkey = N'HKEY_LOCAL_MACHINE', 
  @key = N'SYSTEM\CurrentControlSet\Services\SQLServerAgent', 
  @value_name = N'ObjectName', 
  @value = @SQLAgentAccount output 

SELECT @@SERVERNAME           AS SQLInstance, 
       @DatabaseEngineAccount AS DatabaseEngineServiceAccount, 
       @SQLAgentAccount       AS SQLAgentServiceAccount

The output from above query is shown below.

1_script to find sql server service account


Another way is to query sys.dm_server_services view. This gives the details for all installed SQL Server instances. It is available from SQL Server 2008 R2 SP1.

2_script to find sql server service account

Like us on FaceBook  |  Join the fastest growing SQL Server group on FaceBook


Leave a Reply

Your email address will not be published.