Forum Discussion
GrantBradley
Apr 18, 2019Copper Contributor
Device - Removal
hi Guys Hope someone can help i am looking to removed retired devices from Intune and from Azure AD , i know they are a powershell script any advise would be great , even if you can point a scri...
almennn
Apr 21, 2019Brass Contributor
Hi GrantBradley,
As Ethan Stern said, device cleanup rules are a great way of getting rid of stale devices from Intune and devices which has been unenrolled are automatically deleted from Intune.
Another way of deleting stale devices is via Intune PowerShell SDK. Before using this you have to install the module,
Install-Module -Name Microsoft.Graph.Intune
Here is a simple example of removing devices which has not synced for 60 days:
PS C:\Windows\system32> $device = Get-IntuneManagedDevice -Select lastSyncDateTime, id | Where {($_.lastSyncDateTime -lt (Get-Date).AddDays(-60))} | foreach {$_.id}
PS C:\Windows\system32> ForEach-Object {Remove-IntuneManagedDevice -managedDeviceId $device}
To delete stale objects from AzureAD see this docs page:
https://docs.microsoft.com/sv-se/azure/active-directory/devices/manage-stale-devices