startswith / endswith

startswith checks that a string begins with some text; endswith checks how it ends. Like has and contains, they ignore case by default.

DeviceProcessEvents
| where FileName endswith ".ps1" or ProcessCommandLine startswith "powershell"

Variants

Operator Meaning
startswith, endswith begins / ends with, any case
startswith_cs, endswith_cs the same, exact case
!startswith, !endswith does not begin / end with
!startswith_cs, !endswith_cs does not, exact case

Typical uses

  • Paths: FolderPath startswith @"C:\Users\" (files under user profiles).
  • File types: FileName endswith ".dll".
  • Naming conventions: service accounts that start with svc_, devices whose names start with lt-.
  • IP ranges, roughly: IPAddress startswith "10." (for exact ranges, real Kusto has ipv4_is_in_range(), which this site does not support yet).

Verbatim strings

In a normal string, a backslash starts an escape sequence, so a Windows path needs doubled backslashes: "C:\\Windows". Put @ before the string to take every character literally: @"C:\Windows".

Examples

1 Programs started from under Program Files
DeviceProcessEvents
| where FolderPath startswith @"C:\Program Files"
| distinct FileName, FolderPath

@"..." is a verbatim string: backslashes are taken literally, which is handy for Windows paths.

2 Count the laptops by their name prefix
DeviceProcessEvents
| where DeviceName startswith "lt-"
| summarize Laptops = dcount(DeviceName)
3 Case-insensitive and case-sensitive endswith
DeviceProcessEvents
| summarize AnyCase = countif(FileName endswith ".exe"), ExactCase = countif(FileName endswith_cs ".exe")

Office programs are logged as WINWORD.EXE, so only the case-insensitive version counts them.

Practice it

None of the free exercises needs this one yet.