Skip to content
Menu

Stale or Duplicate Devices in Intune

Lists every Intune device that has not synced for StaleDays days or that is an older record of a serial number seen again, and alerts when it lists any. Read-only.

Stale or duplicate devices in Intune is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Stale or duplicate devices in Intune. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, 11, Server 2016+, with the app certificate; runs as SYSTEM or a user
NeedsPowerShell 5.1; Graph: DeviceManagementManagedDevices.Read.All; an Intune license
Read-onlyReads, changes nothing

What It Does

This script is read-only. It lists every managed device in the tenant's Intune through Microsoft Graph, following every page, and writes one row for each device that is stale, a duplicate or both. A device is stale when its last sync is older than StaleDays (1 to 3650, default 30) or it has never synced.

With CheckDuplicates true, the default, the script groups records by serial number. It ranks them by most recent sync, then most recent enrollment when sync times tie, and flags all but the first record in each group as duplicates. Blank serial numbers and placeholders such as 0, None, Default string, System Serial Number and To be filled by O.E.M. are never treated as duplicates.

Devices named in ExcludeDevices, by exact name, are never listed. The run ends ALERT when it lists anything and OK otherwise, and the answer counts the stale and duplicate devices among all devices. It does not remove anything. A second run reads Intune again and lists what is still there.

A large tenant makes the listing slow, because every device is read and the dates are compared here.

Before You Run It

  • Create an app registration with DeviceManagementManagedDevices.Read.All and grant admin consent. The tenant needs an Intune license.
  • Put the app's certificate, with its private key, on the computer that runs it.
  • Set StaleDays to suit your fleet: a laptop on long leave will show as stale.
  • Add test or lab devices you want left out to ExcludeDevices.
  • Check each listed device before you retire it in Intune, because the script does not remove anything.
  • Schedule it in your RMM and alert on exit 1.

Type and Risk

  • Type: Monitor. It checks one condition. It exits 1 when it finds the problem, so your RMM can alert on it, 0 when all is well, and 2 when it could not run.
  • Risk: Low. It reads and changes nothing.
  • Restart: It never restarts the machine.
  • Typical run: Under 2 minutes (longer for tens of thousands of devices).
  • Example answer: "Intune: 14 stale device(s) (no sync in 30+ days) and 3 older duplicate(s) among 412 devices"
  • Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.

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: A device is stale when it has not synced for more than this many days. Whole number, 1 to 3650. Default 30.
  • CheckDuplicates: true (default) also lists older records that share a serial number with a newer one.
  • ExcludeDevices: Device names never to list, separated by comma, semicolon or new line (exact names). Default empty.

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.

The Script

test-stale-intune-devices.ps1 · 1051 lines · 57.4 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.