Find ActiveSync User by Device ID or IMEI

Have you ever needed to find out which user is using a given mobile device and all you have is the Device ID or IMEI code? The other way around is very simple but doesn’t really solve the problem.

I put together the following piece of code to simplify the operation:

$Mailboxes = Get-CASMailbox -ResultSize Unlimited | Where {$_.HasActiveSyncDevicePartnership -eq $True }
$User = $Mailboxes | %{Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity} | `?{$_.DeviceImei -like "0123456789ABCDEF"}
$User | fl Mailbox, DeviceType

You can of course substitute $_.DeviceImei with $_.DeviceId if that’s what you’re looking for.

Enjoy :)