Get all Mobile Devices on Office 365

In the past I have described how we can delete old EAS devices from our Exchange on-premises and/or Exchange online environment. More about that you can find HERE.

However, sometimes we just have to know how many and which devices are using ActiveSync on our Office 365 tenant.

In this article I will show you two simple ways, how we can do it!
In both cases we will use PowerShell to do it and in both cases we need (like always if we work with PowerShell on our tenant) to connect to our cloud. This we can do with following command:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

After that we can get a simple overview about the mobile devices by using the following command:

Get-Mailbox -ResultSize unlimited | ForEach {Get-ActivesyncDeviceStatistics -Mailbox:$_.Identity | fl}

As you can see in the picture below, we got some first information about our Mobile devices using ActiveSync.

So far so good, now we got a first view about the device we have in our environment. Unfortunately, this will be not enough for further tasks, this because we do not have enough information about Devices. We have no clue to whom it belongs and other additional information.

To get information like device ID, device type, etc. we need to create a PowerShell script. Don’t worry, in this case there is no rocket scenes needed… One way how script could look like you can see in the example below:

#Get Office365 credentials
$UserCredential = Get-Credential

#Set output Path
$OutputPath = Read-Host "Enter Path where the result should be saved! e.g. D:\Report"

#Connect to Exchange online
Write-Host "Connecting to Exchange online..." -ForegroundColor magenta 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Write-Host "Done!" -ForegroundColor green

#Retrieve all mailboxes in the Exchange organization 
Write-Host "Gathering data, Please Wait.." -ForegroundColor magenta
$mailboxes = Get-Mailbox -ResultSize unlimited 
 
#Loop through each mailbox 
foreach ($mailbox in $mailboxes) {

$devices = Get-ActiveSyncDeviceStatistics -Mailbox $mailbox.samaccountname 
 
 #If the current mailbox has an ActiveSync device associated, loop through each device 
 if ($devices) { 
 foreach ($device in $devices){ 
 
 #Create a new object and add custom note properties for each device. Comment out the ones you don't need
 $deviceobj = New-Object -TypeName psobject 
 $deviceobj | Add-Member -Name DisplayName -Value $mailbox.DisplayName -MemberType NoteProperty 
 $deviceobj | Add-Member -Name UPN -Value $mailbox.UserPrincipalName -MemberType NoteProperty 
 $deviceobj | Add-Member -Name Status -Value $device.Status -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceID -Value $device.DeviceID -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceEnableOutboundSMS -Value $device.DeviceEnableOutboundSMS -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceMobileOperator -Value $device.DeviceMobileOperator -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceAccessState -Value $device.DeviceAccessState -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceAccessStateReason -Value $device.DeviceAccessStateReason -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceAccessControlRule -Value $device.DeviceAccessControlRule -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceType -Value $device.DeviceType -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceUserAgent -Value $device.DeviceUserAgent -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceModel -Value $device.DeviceModel -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceFriendlyName -Value $device.DeviceFriendlyName -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceOS -Value $device.DeviceOS -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceOSLanguage -Value $device.DeviceOSLanguage -MemberType NoteProperty 
 $deviceobj | Add-Member -Name IsRemoteWipeSupported -Value $device.IsRemoteWipeSupported -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceWipeSentTime -Value $device.DeviceWipeSentTime -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceWipeRequestTime -Value $device.DeviceWipeRequestTime -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceWipeAckTime -Value $device.DeviceWipeAckTime -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastDeviceWipeRequestor -Value $device.LastDeviceWipeRequestor -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DevicePolicyApplied -Value $device.DevicePolicyApplied -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DevicePolicyApplicationStatus -Value $device.DevicePolicyApplicationStatus -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceActiveSyncVersion -Value $device.DeviceActiveSyncVersion -MemberType NoteProperty 
 $deviceobj | Add-Member -Name FirstSyncTime -Value ($device.FirstSyncTime).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastPolicyUpdateTime -Value ($device.LastPolicyUpdateTime).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastSyncAttemptTime -Value ($device.LastSyncAttemptTime).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastSuccessSync -Value ($device.LastSuccessSync).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name NumberOfFoldersSynced -Value $device.NumberOfFoldersSynced -MemberType NoteProperty 
 
 #Write the custom object to the pipeline 
 Write-Output -InputObject $deviceobj 


# Out-File -FilePath '$OutputPath\ActiveSyncUsers.csv' -InputObject $deviceobj -Encoding UTF8 -append
 Export-Csv '$OutputPath\ActiveSyncUsers.csv' -InputObject $deviceobj -Encoding UTF8 -append
 
 } 
 
 } 
 
}

The result we get on the screen will look like this:

As you can see in the script, we will get as well as CSV-File, which will contain the same information, we got presented on the command line.

Note: In this script I am still using the old command Get-ActiveSyncDeviceStatistics. We also can use new cmdlet Get-MobileDeviceStatistics instead.

The script will look like this:

#Get Office365 credentials
$UserCredential = Get-Credential

#Set output Path
$OutputPath = Read-Host "Enter Path where the result should be saved! e.g. D:\Report"

#Connect to Exchange online
Write-Host "Connecting to Exchange online..." -ForegroundColor magenta 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Write-Host "Done!" -ForegroundColor green

#Retrieve all mailboxes in the Exchange organization 
Write-Host "Gathering data, Please Wait.. (This can take a wile)" -ForegroundColor magenta
$mailboxes = Get-Mailbox -ResultSize unlimited 
 
#Loop through each mailbox 
foreach ($mailbox in $mailboxes) {

$devices = Get-MobileDeviceStatistics -Mailbox $mailbox.samaccountname 
 
 #If the current mailbox has an ActiveSync device associated, loop through each device 
 if ($devices) { 
 foreach ($device in $devices){ 
 
 #Create a new object and add custom note properties for each device. Comment out the ones you don't need
 $deviceobj = New-Object -TypeName psobject 
 $deviceobj | Add-Member -Name DisplayName -Value $mailbox.DisplayName -MemberType NoteProperty 
 $deviceobj | Add-Member -Name UPN -Value $mailbox.UserPrincipalName -MemberType NoteProperty 
 $deviceobj | Add-Member -Name Status -Value $device.Status -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceID -Value $device.DeviceID -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceEnableOutboundSMS -Value $device.DeviceEnableOutboundSMS -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceMobileOperator -Value $device.DeviceMobileOperator -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceAccessState -Value $device.DeviceAccessState -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceAccessStateReason -Value $device.DeviceAccessStateReason -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceAccessControlRule -Value $device.DeviceAccessControlRule -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceType -Value $device.DeviceType -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceUserAgent -Value $device.DeviceUserAgent -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceModel -Value $device.DeviceModel -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceFriendlyName -Value $device.DeviceFriendlyName -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceOS -Value $device.DeviceOS -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceOSLanguage -Value $device.DeviceOSLanguage -MemberType NoteProperty 
 $deviceobj | Add-Member -Name IsRemoteWipeSupported -Value $device.IsRemoteWipeSupported -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceWipeSentTime -Value $device.DeviceWipeSentTime -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceWipeRequestTime -Value $device.DeviceWipeRequestTime -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceWipeAckTime -Value $device.DeviceWipeAckTime -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastDeviceWipeRequestor -Value $device.LastDeviceWipeRequestor -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DevicePolicyApplied -Value $device.DevicePolicyApplied -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DevicePolicyApplicationStatus -Value $device.DevicePolicyApplicationStatus -MemberType NoteProperty 
 $deviceobj | Add-Member -Name DeviceActiveSyncVersion -Value $device.DeviceActiveSyncVersion -MemberType NoteProperty 
 $deviceobj | Add-Member -Name FirstSyncTime -Value ($device.FirstSyncTime).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastPolicyUpdateTime -Value ($device.LastPolicyUpdateTime).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastSyncAttemptTime -Value ($device.LastSyncAttemptTime).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name LastSuccessSync -Value ($device.LastSuccessSync).ToString("yyyy-MM-dd HH:mm:ss") -MemberType NoteProperty 
 $deviceobj | Add-Member -Name NumberOfFoldersSynced -Value $device.NumberOfFoldersSynced -MemberType NoteProperty 
 
 #Write the custom object to the pipeline 
 Write-Output -InputObject $deviceobj 


# Out-File -FilePath '$OutputPath\ActiveSyncUsers.csv' -InputObject $deviceobj -Encoding UTF8 -append
 Export-Csv '$OutputPath\ActiveSyncUsers.csv' -InputObject $deviceobj -Encoding UTF8 -append
 
 } 
 
 } 
 
}

As I have promised you, this script has nothing to do with rocket Science. We simply had to collect all information, build a loop to get only the outputs we want to have and in the end we paste it to a csv file. Use the small script and/or change it in any ways you need. At the end of the day it should be a little help for you.

 

 

 


Leave a Reply

Your email address will not be published. Required fields are marked *