Explained : Get List of devices connected to Mailbox (Office 365)

2020/05/05 | 1 minute read |

Test environment Powershell v.5

Script to get List of devices connected to Mailbox (Office 365) explained

This script returns all the Exchange ActiveSync mobile devices that are users has used that are associated with their mailboxes.

Link : My Office 365 Repository

Author: Diego Messiah

First Part - Connection to the Tenant

 $credentials = Get-Credential -Credential Admin@ACME.com
 Write-Output "Getting the Exchange Online cmdlets"
    $session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
        -ConfigurationName Microsoft.Exchange -Credential $credentials `
        -Authentication Basic -AllowRedirection
    Import-PSSession $session

Second Part - Setup Variable and CSV.

#Location of CSV
$csv = ".\MobileDevices.csv" 
$results = @()
#The default quota is for 1000 items
$mailboxUsers = get-mailbox -resultsize unlimited
$mobileDevice = @()

Third Part - Get the information

We get relevant information like UPN (User ID or User Principal Name), Type of program, First Sync Time (Date and hour of the mailbox first sync)

foreach($user in $mailboxUsers)
$UPN = $user.UserPrincipalName
$mobileDevices = Get-MobileDevice -Mailbox $UPN

      foreach($mobileDevice in $mobileDevices)
          Write-Output "Getting info about a device for $user"
          $properties = @{
          Name = $user.name
          UserDisplayName = $mobileDevice.UserDisplayName
          UPN = $UPN
          ClientType = $mobileDevice.ClientType
          DeviceModel = $mobileDevice.DeviceModel
          DeviceOS = $mobileDevice.DeviceOS
          DeviceTelephoneNumber = $mobileDevice.DeviceTelephoneNumber
          FirstSyncTime = $mobileDevice.FirstSyncTime
          IsValid = $mobileDevice.IsValid
          ExchangeObjectId = $mobileDevice.ExchangeObjectId 
          IsManaged = $mobileDevice.IsManaged
          IsCompliant = $mobileDevice.IsCompliant
          IsDisabled = $mobileDevice.IsDisabled
          $results += New-Object psobject -Property $properties

Fourth Part - Order the info

$results | Select-Object Name,UserDisplayName,UPN,ClientType,DeviceModel,DeviceOS,DeviceTelephoneNumber,FirstSyncTime,IsValid,ExchangeObjectId,IsManaged,IsCompliant,IsDisabledName | Export-Csv -notypeinformation -Path $csv

And last but not least - Close the connection

Remove-PSSession $session

The complete script is in My Office 365 Repository

Leave a comment