What It Does
Risk is High, so the script lists what it would delete and deletes nothing unless you set DryRun=false and Approve=remove-stale-intune-devices. It selects records with no sync within StaleDays and, with RemoveDuplicates on, flags extra records sharing a serial number, ranked by sync time and then enrollment time. Blank and placeholder serials are ignored for duplicate checks. The monitor defaults to 30 days. This action defaults to 90 and also protects recently synced records.
A device that synced in the last 7 days is never selected, and devices in ExcludeDevices are skipped. When every device in the tenant looks stale it stops and deletes nothing, because that points to a sync problem, not a clean-up. MaxChanges (default 25) caps one run.
Before deleting, the script writes the selected records, including their IDs, serials, users and last sync, to a JSON file. It deletes Intune records without sending a wipe or retire. With IncludeEntraDevice it also deletes the associated Entra device if no Intune record outside the deletion plan referenced it when the run built the plan. The read-back checks the Intune records only.
Partly tested: the selection is the tested monitor's own code and the delete call was run against a test tenant, but a full run that deletes a 7-day-old record has not happened yet. Start with a dry run and a small MaxChanges.
Before You Run It
- Create an app registration in the client tenant with the Graph application permission DeviceManagementManagedDevices.ReadWrite.All (and Device.ReadWrite.All for IncludeEntraDevice), and give admin consent.
- Run test-stale-intune-devices with the same StaleDays setting for an initial review. Add devices to keep to ExcludeDevices and confirm the final targets in DryRun.
- A deleted record cannot be restored; a device that is still alive re-enrols and creates a new one.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=remove-stale-intune-devices, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Deletes the Intune records of stale and duplicate devices (and optionally their Entra ID devices); saves the records to a JSON file first.
- Restart: It never restarts the machine.
- Typical run: Under 5 minutes.
- Example answer: "Deleted 11 Intune device record(s) (3 of them duplicates); the records were saved to a before-state file first"
- Partly tested before listing: run with Windows PowerShell 5.1 against a Microsoft 365 test tenant as far as it allowed, and compared with tested scripts. The help says which part is unproven.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- TenantId: The tenant ID (a GUID) or a domain such as contoso.onmicrosoft.com.
- ClientId: The app registration's Application (client) ID, a GUID.
- CertificateThumbprint: Thumbprint of the app's certificate (with its private key) in LocalMachine\My or CurrentUser\My on this computer. Leave empty only if you use the ClientSecret environment variable.
- StaleDays: Delete devices that have not synced for more than this many days. Whole number, 7 to 3650. Default 90.
- RemoveDuplicates: true (default) also deletes older records that share a serial number with a newer one.
- ExcludeDevices: Device names never to delete, separated by comma, semicolon or new line (exact names). Default empty.
- IncludeEntraDevice: true also deletes the matching Microsoft Entra ID device object. Default false (Intune record only).
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.