$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $M365Cred -Authentication Basic -AllowRedirection Import-PSSession $Session -DisableNameChecking ## Get All Devices and Mailboxes metadata, store in memory/variable ## Torey should verify I'm pulling ActiveSync devices correctly: #$AllDevices = Get-MobileDevice # All Devices $AllDevices = Get-MobileDevice -ActiveSync # Only ActiveSync I hope $AllMailboxes = Get-Mailbox -ResultSize Unlimited # Loop through each device, get a part of its identity, and match that identity part with a mailbox, 2400 devices took about 20 minutes to run $result = @() $count = 0 ForEach ($Device in $AllDevices) { $count++ write-warning ($count.tostring() + '/' + $AllDevices.Count) #clear loop variables $Mailbox = $null $DeviceDisplayName = $null $ADUser = $null $DeviceDisplayName = $Device.Identity.split('\')[0] $Mailbox = $AllMailboxes | Where-Object DisplayName -eq $DeviceDisplayName If($Mailbox){ $ADUser = Get-ADUser -Identity $Mailbox.PrimarySmtpAddress.split('@')[0] -Properties CanonicalName } # create custom object to show only information we want $obj = [PSCustomObject]@{ DeviceDisplayName = $DeviceDisplayName; DeviceAccessState = $Device.DeviceAccessState; MailboxName = If($Mailbox){$Mailbox.Name}; PrimarySmtpAddress = If($Mailbox){$Mailbox.PrimarySmtpAddress}; Agency = If($Mailbox){$ADuser.CanonicalName.split('/')[1]}; DeviceFriendlyName = $Device.FriendlyName; DeviceUserAgent = $Device.DeviceUserAgent; } # store new object in array $result += $obj } Write-Output $result