Query Details
IdentityInfo | where Timestamp > ago(30d) | where Type == @"ServiceAccount" | extend ["Service On-Prem Sid"] = OnPremObjectId | extend ["Service Principal Name"] = iff(IdentityEnvironment == "OnPremises",replace_string(strcat(AccountName, "@",AccountDomain),"$",""), AccountUpn ) | where parse_json(UserAccountControl)[0] != 'WorkstationTrustAccount' //Exclude gMSA/dMSA | where not (ChangeSource == @"System-UserPersistence" and isempty(CloudSid)) //Exclude Service Principals | summarize arg_max(Timestamp,*) by AccountObjectId,CloudSid,['Service On-Prem Sid']
This KQL query is designed to filter and summarize information about service accounts from the IdentityInfo table. Here's a breakdown in simpler terms:
Time Filter: It selects records from the last 30 days (Timestamp > ago(30d)).
Type Filter: It focuses specifically on entries where the Type is "ServiceAccount".
Data Extension:
OnPremObjectId field.AccountName and AccountDomain, removing any trailing "$".AccountUpn.Account Type Exclusion: It excludes accounts identified as 'WorkstationTrustAccount' by checking the UserAccountControl field.
Service Principal Exclusion: It excludes entries where the ChangeSource is "System-UserPersistence" and CloudSid is empty, which typically represents service principals.
Summarization: Finally, it summarizes the data to keep only the most recent entry (arg_max(Timestamp,*)) for each unique combination of AccountObjectId, CloudSid, and "Service On-Prem Sid".
In essence, this query filters and processes service account data to provide a concise summary of the most recent relevant entries, excluding certain types of accounts and service principals.

Jay Kerai
Released: June 23, 2026
Tables
Keywords
Operators