Query Details
# Defender for Endpoint - Active - Inactive Devices


## Query Information
### Description
This query allows to identify active and non-active devices.
#### References
### Author
- **Alex Verboon**
## Defender XDR
```kql
let ActiveThresholdDays = 30;
let OS = dynamic(["Windows10","Windows11"]);
DeviceInfo
| where TimeGenerated > ago(30d)
| where OSPlatform has_any (OS)
| where OnboardingStatus == 'Onboarded'
| summarize arg_max(TimeGenerated,*) by DeviceId
| extend LastSeen = Timestamp
| extend DaysSinceLastSeen = datetime_diff("day", now(), LastSeen)
| extend DynamicTagsArray = iif(isnull(DeviceDynamicTags),
dynamic([]), todynamic(DeviceDynamicTags))
| project TimeGenerated,LastSeen, DaysSinceLastSeen,DeviceName, OSPlatform, MachineGroup, DynamicTagsArray
// Show all active devices
//| where DaysSinceLastSeen <= ActiveThresholdDays
// Show all inactive devices
| where DaysSinceLastSeen >= ActiveThresholdDays
```
With state column
```kql
let ActiveThresholdDays = 30;
let OS = dynamic(["Windows10","Windows11"]);
DeviceInfo
| where TimeGenerated > ago(30d)
| where OSPlatform has_any (OS)
| where OnboardingStatus == 'Onboarded'
| summarize arg_max(TimeGenerated,*) by DeviceId
| extend LastSeen = Timestamp
| extend DaysSinceLastSeen = datetime_diff("day", now(), LastSeen)
| extend DynamicTagsArray = iif(isnull(DeviceDynamicTags),
dynamic([]), todynamic(DeviceDynamicTags))
| extend State = iif(DaysSinceLastSeen <= ActiveThresholdDays, "🟢 Active", "⚪ Inactive")
| project TimeGenerated, LastSeen, DaysSinceLastSeen, State,
DeviceName, OSPlatform, MachineGroup, DynamicTagsArray
```This KQL query is designed to identify and categorize devices as either active or inactive based on their last seen activity within Microsoft Defender for Endpoint. Here's a simplified breakdown of what the query does:
Set Parameters:
ActiveThresholdDays is set to 30, meaning a device is considered active if it has been seen within the last 30 days.Filter Data:
Identify Latest Activity:
Calculate Days Since Last Seen:
Tag Devices:
Determine Device State:
Output Results:
This query helps in monitoring device activity and ensuring that all devices are accounted for and properly managed within the organization's network.

Alex Verboon
Released: December 9, 2025
Tables
Keywords
Operators