List email addresses for ActiveSync device associations in Office 365

Today I had to get a list of email addresses for iOS users associated with a specific Office 365 tenant.

Using the Get-MobileDevice cmdlet in Exchange/Office 365 PowerShell is handy, but it only shows the user’s name, not their email address:

There are some older solutions around on the web that involve running a Get-Mailbox, and then iterating through each mailbox to get the ActiveSync device information. This seemed like overkill to me, as I only needed a basic list.

I ended up with the following one-liner, which uses Calculated Properties to grab the email address:

Get-MobileDevice -ResultSize Unlimited | Select-Object @{Name='User';Expression={(Get-Mailbox -Identity $_.UserDisplayName) | Select-Object -expand WindowsEmailAddress}},DeviceID,DeviceImei,DeviceOS,DeviceType,DeviceUserAgent,DeviceModel | Export-Csv C:\temp\mobile_devices.csv

This allowed me to open the CSV in Excel and filter down the list until I was left with the information that we were after.

Your mileage may vary using this command, as we’re matching a ‘UserDisplayName’ field on a Microsoft.Exchange.Data.Directory.SystemConfiguration.MobileDevice to the ‘Identity’ field on a Microsoft.Exchange.Data.Directory.Management.Mailbox.

Office 365/Azure AD: Find all users with an email address that matches a specific string

We have a lot of shared mailboxes in Office 365. Today, I needed to find a subset of those,  change their UPN, and set the FirstName and LastName attributes.

I came across this lengthy PowerShell script that someone had created years ago to find users, but the simplest way to do this is via this one line of PowerShell, once you’ve connected to Azure AD:

Get-MsolUser -MaxResults 1000 | Where-Object {($_.ProxyAddresses -like '*texttomatch*')}

An alternative method, if you’re not connected to Azure PowerShell, but are connected to Exchange Online via PowerShell, is to use Get-Mailbox:

Get-Mailbox -ResultSize 1000 | ? {$_.EmailAddresses -like '*texttomatch*'}

Helper Function for Connecting to Exchange Online PowerShell

I’ve been doing a bit of work against Office 365 and Exchange Online recently as part of a migration from Exchange 2010 to Office 365.

Here’s a couple of basic helper functions that I’ve included in my PowerShell profile to make connecting to Exchange Online a little easier:

function Connect-ExchangeOnline() {
    $upn = ([ADSISEARCHER]"samaccountname=$($env:USERNAME)").Findone().Properties.userprincipalname
    $creds = Get-Credential -UserName $upn -Message "Enter password for $upn"
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $creds -Authentication Basic -AllowRedirection

    Import-PSSession $session

    Write-Output "`n`n`nDon't forget to 'Remove-PSSession `$session' or 'Disconnect-ExchangeOnline' when you're done"

function Disconnect-ExchangeOnline() {
    if ($session) { 
        Remove-PSSession $session
    } else {
        Get-PSSession | Remove-PSSession

    "$((Get-PSSession | Measure-Object).Count) PowerShell session(s)"

What the Connect-ExchangeOnline function does is build on Microsoft’s own instructions to automatically detect the current user’s UPN, and use that in the prompt for credentials:

PowerShell Prompt showing an authentication popup that has the username field pre-filled with the current user's UPN

After that, it’s business as usual:


I also have a similar set of helper functions that I use for connecting to Exchange on-prem.