Entra Stale Device Cleanup Automation

Automated lifecycle management for stale Microsoft Entra devices using certificate-based Graph authentication.

The Problem

Stale devices were accumulating in Microsoft Entra with no MDM in place at the time, so the only way to clean them up was manually through the Entra portal. The process was slow, entirely manual, and consumed significant administrator time. Stale device records also represented unnecessary exposure, since a stale device object can still hold access to resources.

What I Built

A PowerShell automation that runs as a scheduled task on a server and removes Entra devices whose last activity falls outside a configurable staleness threshold. It authenticates to Microsoft Graph with certificate-based app-only authentication, so it runs unattended with no stored secrets and no interactive sign-in.

How It Works

Why the Exclusion Group

Some locations use shared devices that can legitimately sit inactive for long periods and cross the staleness threshold without being abandoned. Those devices must be available immediately when needed, so they are placed in a protection group and excluded from cleanup entirely.

Impact

  • Removes 100 to 2,000 stale device records per run
  • Eliminates hours of manual portal cleanup per cycle
  • Reduces exposure risk from stale device objects that retain access to resources
  • Provides a persistent audit trail of every deletion

Script

View the full script →