As people upgrade their mobile devices connected to their Exchange Online mailbox the devices are left connected to their mailbox in Exchange Admin Console.
The powershell script below will check all the mobile devices connected to Exchange mailboxes and remove any not successfully synchronised for 120 days.
# Remove Mobile devices not synced for 120 days
$Office365Credentials = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Office365credentials -Authentication Basic –AllowRedirection
Import-PSSession $Session -AllowClobber | Out-Null
$devices = Get-MobileDevice -resultsize unlimited
foreach ($device in $devices) {
$ds = Get-MobileDeviceStatistics -identity $device.id
If ($ds.LastSuccessSync -lt (Get-Date).AddDays(-120)) {
Write-host $device.UserDisplayName $device.deviceos $ds.LastSuccessSync " removing..." -foreground red
Remove-MobileDevice $device.id -confirm:$false
}
else {
Write-host $device.UserDisplayName $device.deviceos $ds.LastSuccessSync " good" -foreground green
}
}
Running the script periodically or on a schedule should help with housekeeping.