TSQL – Get Service Account Details

From time to time I find myself needing to check on the service account for one reason or another, and I can never remember the correct DMV’s and always go fishing for the details. In the spirit of putting things I keep doing on my blog so I have a place to find them, here is the correct DMV:

sys.dm_server_services

and to go a step further this is the code I use:

select
  servicename
  ,startup_type_desc
  ,service_account
from
  sys.dm_server_services

This should work on any server from SQL 2008 R2 onwards, so hopefully any server you are working on. However if you ARE working on a 19+ year old server, here’s how to do it on SQL 2005 and SQL 2008(and SQL 2008 R2 prior to SP1):

DECLARE @servaccount NVARCHAR(128);
–SQL Agent
EXEC master.dbo.xp_regread
‘HKEY_LOCAL_MACHINE’,
‘SYSTEM\CurrentControlSet\services\SQLSERVERAGENT’,
‘ObjectName’,
@servaccount OUTPUT;

SELECT @servaccount as AGENT_ServiceAccount;

–SQL Server
EXEC master.dbo.xp_regread
‘HKEY_LOCAL_MACHINE’,
‘SYSTEM\CurrentControlSet\services\MSSQLSERVER’,
‘ObjectName’,
@servaccount OUTPUT;

SELECT @servaccount as SQL_ServiceAccount;

Leave a Reply

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