Using startswith and endswith
Sometimes you are not searching for a word anywhere in the field.
You are looking for how the value begins.
Or how it ends.
That small detail can separate useful evidence from a log pile big enough to make a hedgehog need a coffee break.
A suspicious filename may end with .ps1. A URL may start with http://. A command line may begin with a trusted-looking executable and end with a risky argument.
In this Agent Foskett Academy lesson, you will learn how defenders use the KQL startswith and endswith operators to find prefixes and endings across Microsoft Defender XDR and Microsoft Sentinel telemetry.
Lesson overview
Learn how prefix and ending searches help defenders find suspicious filenames, domains, URLs and command-line behaviour faster.
Why startswith and endswith matter
That is useful, but sometimes you need a more focused question:
Does this value start with a certain pattern? Does this filename end with a suspicious extension? Does this URL begin with an insecure or unusual prefix?
Investigation scenario
The user clicked a suspicious link, then a script appeared on the device. The analyst wants to find script files, suspicious download paths and URLs that start with risky prefixes.
Rather than searching every field with broad contains filters, the analyst uses startswith and endswith to ask more precise questions.
Step 1 — Find script files with endswith
- 1
- 2
- 3
- 4
- 5
- 6
DeviceFileEvents | where Timestamp > ago(7d) | where FileName endswith ".ps1" | project Timestamp, DeviceName, ActionType, FileName, FolderPath, InitiatingProcessCommandLine | sort by Timestamp desc
Step 2 — Find URLs that start with a risky prefix
- 1
- 2
- 3
- 4
- 5
- 6
UrlClickEvents | where Timestamp > ago(14d) | where Url startswith "http://" | project Timestamp, AccountUpn, Url, ActionType, ThreatTypes, NetworkMessageId | sort by Timestamp desc
Step 3 — Find command lines that start with PowerShell
- 1
- 2
- 3
- 4
- 5
- 6
DeviceProcessEvents | where Timestamp > ago(7d) | where ProcessCommandLine startswith "powershell" | project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine | sort by Timestamp desc
What the operators do
The endswith operator checks whether a value finishes with the text you provide.
Both operators are useful when the position of the text matters.
Step 4 — Find suspicious file endings
- 1
- 2
- 3
- 4
- 5
- 6
- 7
DeviceFileEvents | where Timestamp > ago(30d) | where FileName endswith ".js" or FileName endswith ".vbs" or FileName endswith ".ps1" | project Timestamp, DeviceName, ActionType, FileName, FolderPath, InitiatingProcessAccountName | sort by Timestamp desc
Step 5 — Find domain suffixes
- 1
- 2
- 3
- 4
- 5
- 6
EmailEvents | where Timestamp > ago(30d) | where SenderFromDomain endswith ".ru" | project Timestamp, SenderFromAddress, RecipientEmailAddress, Subject, DeliveryAction, ThreatTypes | sort by Timestamp desc
Step 6 — Use parameters for repeatable searches
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
let urlPrefix = "http://"; let scriptEnding = ".ps1"; DeviceProcessEvents | where Timestamp > ago(14d) | where ProcessCommandLine startswith "powershell" or ProcessCommandLine endswith scriptEnding | project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine | sort by Timestamp desc
Investigator notes
Do not forget that attackers can rename files, alter paths and change command formatting. These operators are excellent for focused hunting, but they should sit alongside broader searches when you are still exploring.
What you learned
Continue your investigation
Continue learning with Using has_any, Using in, KQL Threat Hunting Guide and Microsoft Security.
Develop IT. Protect IT. GEMXIT PTY LTD | GEMXIT UK LTD