Defender for Identity - Service Accounts
MDI Service Accounts
Query
IdentityInfo
| where Timestamp > ago(30d)
| where Type == @"ServiceAccount"
| where SourceProvider == @"ActiveDirectory"
| summarize arg_max(Timestamp,*) by OnPremSidAbout this query
Explanation
This query is designed to extract and list Active Directory Service Accounts using Microsoft Defender for Identity. Here's a simple breakdown of what it does:
-
Purpose: The query aims to identify and list service accounts from Active Directory, which are managed by Microsoft Defender for Identity. These accounts include:
- gMSA (Group Managed Service Accounts): Used for services requiring mutual authentication across multiple servers.
- sMSA (Managed Service Accounts): Used for individual services on a single server.
- User Accounts: Regular user accounts configured with "Password never expires" and a Service Principal Name (SPN) are also considered service accounts.
-
Query Breakdown:
-
The first part of the query filters the
IdentityInfotable to find entries from the last 30 days (Timestamp > ago(30d)) that are classified as "ServiceAccount" and originate from "ActiveDirectory". It then summarizes the most recent entry for each service account usingarg_max(Timestamp,*)byOnPremSid(On-Premises Security Identifier). -
The second part of the query further processes the
IdentityInfotable to summarize the most recent entry for each account byAccountName. It extracts the Organizational Unit (OU) path from theDistinguishedNamefield and filters for those containing "OU=ServiceAccounts". Finally, it lists distinct OU paths where service accounts are located.
-
-
Outcome: The result is a list of Active Directory service accounts and their organizational units, helping administrators identify and manage these accounts effectively.
Details

Alex Verboon
Released: December 22, 2025
Tables
Keywords
Operators