Since Intune cleanup rules only hide devices from the Intune console, implement separate procedures to clean up stale devices from Microsoft Entra ID for complete environment hygiene.
Access the Microsoft Entra admin center at https://entra.microsoft.com and navigate to Identity > Devices > All devices.
Identify Stale Entra ID Devices:
- Use the filter options to find inactive devices:
- Activity: Set to "Inactive for more than 60 days"
- Join type: Filter by "Azure AD joined" or "Hybrid Azure AD joined"
- Device state: Look for "Enabled" devices that are actually stale
- Export the filtered list for analysis
- Cross-reference with your Intune hidden devices list
Cleanup Procedures by Join Type:
For Azure AD Joined Devices:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Device.ReadWrite.All"
# Get devices inactive for more than 90 days
$cutoffDate = (Get-Date).AddDays(-90)
$staleDevices = Get-MgDevice -All | Where-Object {
$_.ApproximateLastSignInDateTime -lt $cutoffDate -and
$_.TrustType -eq "AzureAd"
}
# Review before deletion
$staleDevices | Select-Object DisplayName, ApproximateLastSignInDateTime, TrustType | Format-Table
# Delete stale devices (uncomment when ready)
# $staleDevices | ForEach-Object { Remove-MgDevice -DeviceId $_.Id }
For Hybrid Azure AD Joined Devices:
- Delete stale computer objects in on-premises Active Directory
- Move stale objects to an OU not synced with Azure AD Connect
- Use Azure AD Connect to sync the deletions to Entra ID
Automated Cleanup Script:
# Weekly cleanup script for Entra ID devices
param(
[int]$DaysInactive = 90,
[switch]$WhatIf = $true
)
Connect-MgGraph -Scopes "Device.ReadWrite.All"
$cutoffDate = (Get-Date).AddDays(-$DaysInactive)
$staleDevices = Get-MgDevice -All | Where-Object {
$_.ApproximateLastSignInDateTime -lt $cutoffDate -and
$_.TrustType -eq "AzureAd" -and
$_.AccountEnabled -eq $true
}
Write-Output "Found $($staleDevices.Count) stale devices older than $DaysInactive days"
if ($WhatIf) {
$staleDevices | Select-Object DisplayName, ApproximateLastSignInDateTime | Format-Table
} else {
$staleDevices | ForEach-Object {
Write-Output "Deleting device: $($_.DisplayName)"
Remove-MgDevice -DeviceId $_.Id
}
}
Pro tip: Always run cleanup scripts with the -WhatIf parameter first to preview what will be deleted. Schedule Entra ID cleanup to run after your Intune cleanup rules have had time to process.
Verification: After running the cleanup, verify that your Entra ID device count has decreased appropriately. Check that no active devices were accidentally removed by reviewing recent sign-in logs. The total device count in Entra ID should now more closely match your active Intune device count.